954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

foreach loop start a new row after ever 3 records

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

data1
data2
data3

data4
data5
data5

here is my loop:

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


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

Thanks

andy106
Newbie Poster
20 posts since Aug 2010
Reputation Points: 10
Solved Threads: 1
 

Have a look at this thread .

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 
<?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
Junior Poster
111 posts since Apr 2011
Reputation Points: 18
Solved Threads: 16
 

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

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: