PHP while loop, table row

Posted by Elliott on Stack Overflow See other posts from Stack Overflow or by Elliott
Published on 2011-01-02T16:31:07Z Indexed on 2011/01/02 16:53 UTC
Read the original article Hit count: 154

Filed under:
|
|

Hi, I'm trying to loop through a database and output data into a div, if there are 3 divs horizontally accross it will the start a new tr until it reaches another 3 etc. It is currently outputted like :

[] [] []
[]
[] []

Currently I have it working when it reaches 3 it starts a new row, but I don't know how I can only echo a new tr once? As it creates a new table row each time.

echo '<table><tr>';  

while ($result) 
{
    $i ++;
    if ($i&3)
    {
      echo '<td>
         <div>Row Data</div>
       </td>'; 
    }

    else
    {       
      echo '<tr>
        <td>
          <div>Row Data</div>
        </td></tr>';
   }
}  
echo '</tr></table>';

The $result variable is the recordset from my sql query, everything works fine I just don't know how to only create a new table row once?

Thanks

© Stack Overflow or respective owner

Related posts about php

Related posts about table