Sql query from database

116 Views Asked by At

I need some help with how I can soft query from sql. I have a php script with an html page who is showing the result from the query.

I have this query:

 $ready_orders = DB::query( 'SELECT * FROM ordertable where orderid is NOT null ORDER by id DESC')->fetchAll( DB::FETCH_ASSOC ); 

From the HTML I have this;

<tal:block tal:condition="exists:orders">
  <table>
    <tr>
      <td>amount</td>
      <td> want counter here</td>
      <td>result 1</td>
      <td>result 2</td>
      <td>result 3</td>
      <td>result 4</td>
    </tr>
    <tr tal:repeat="order orders">
      <td tal:content="">
      ....
      </td>

      <td tal:content="order/id">
      ....
      </td>


      <td tal:content="order/orderid">
      ....
      </td>
      <td tal:content="order/productid">
      ....
      </td>
      <td tal:content="order/processed">
      ....
      </td>

      <td>
      </td>
    </tr>
  </table>

The query is doing all I want, but need the result to be in a listing. Like a counter 1,2,3,4,5 etc

Is it possible?

3

There are 3 best solutions below

2
On

include a

select count * from ordertable

Then insert this result in the first

0
On
SELECT COUNT(orderid) AS Order_Count
FROM ordertable
WHERE orderid IS NOT NULL
3
On

using foreach loop

<tal:block tal:condition="exists:orders">
  <table>
   <tr>
      <td>amount</td>
     <?php 
       $i = 1;
       foreach($ready_orders as $result){ ?>
          <td>result <?php echo $i++; ?></td> //your counter
       <?php } ?>
   </tr>

  <tr tal:repeat="order orders">
    <td tal:content="">
        ....
     </td>

  <td tal:content="order/id">
  ....
  </td>


  <td tal:content="order/orderid">
  ....
  </td>
  <td tal:content="order/productid">
  ....
  </td>
  <td tal:content="order/processed">
  ....
  </td>

  <td>
  </td>
</tr>