son.of.the.morning Posted April 10, 2012 Share Posted April 10, 2012 This sounds stupid, i know. I want to create a foreach loop that will display 3 records per row. For example when you go on play.com and you query something you get a tile view of items. Can anyone help me? Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 10, 2012 Share Posted April 10, 2012 You'll use a counter to determine when it's time to insert a new row in your table. Increment it each time, and use if($counter%3==0) to determine when to do the html for ending/starting the row. Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted April 10, 2012 Author Share Posted April 10, 2012 have you a full example so i can make head and tail of it, please. Sorry to be a pain. Quote Link to comment Share on other sites More sharing options...
xyph Posted April 10, 2012 Share Posted April 10, 2012 <?php // Make dummy array $array = range(0,32); // This will keep track of how many rows we've echo'd $i = 0; // This tells us how many columns $cols = 3; $total = count( $array ); echo '<table><tr>'; foreach( $array as $value ) { // Echo the value in a cell echo '<td style="border:1px solid black;">'.$value.'</td>'; // Increase our counter by 1 before we check $i++; // Check to see if we need to start a new row by using the modulus operator // It'll give us the remainder of the two numbers divided. // We check if $i != $total to make sure we don't have a redundant empty row // at the end if( ($i % $cols) == 0 && $i != $total ) echo '</tr><tr>'; // Start a new row } // Now after the foreach, if we have an odd number of results, we'll have an // extra empty column we need to fill, we can do this automatically while( ($i % $cols) != 0 ) { echo '<td> </td>'; $i++; } echo '</tr></table>'; ?> Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted April 10, 2012 Author Share Posted April 10, 2012 Thank you! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.