Hi i have a foreach loop, but it is just showing everything in one long line, so I wanted to put it in a table like this

<table>
<tr>
<td>data1</td>
<td>data2</td>
<td>data3</td>

</tr>

<tr>
<td>data4</td>
<td>data5</td>
<td>data5</td>

</tr>
</table>

here is my loop:

<?php
    foreach( $products as $product )
    {
        ?>
        <? echo $product ?>
        <br />

I know if I remove the <br /> i will get a long lasting line. the data is being pulled from my DB.

Thanks

Recommended Answers

All 3 Replies

<?php
echo('<table><tr>');
$i = 0;
foreach( $products as $product )
{
   $i++;
   echo '<td>'.$product .'</td>';
   if(i % 3==0)
   {
      echo '</tr><tr>';
   }
}
echo'</tr></table>';
?>

@samueal: If you have exactly 3 products, you'll insert an extra (empty) row.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.