jason360 Posted November 6, 2011 Share Posted November 6, 2011 Hey guys, I am using php loop to echo out all off the data in my database category within a html table. However I would like to reduce each table row to only 5 results then start a new row so that the table fits within the webpage. Any suggestions? Any help is much appreciated. Thanks. Jason Here is what i am using, but does not fit within the page. <table cellspacing="10"> <tr> <?php $query = mysql_query('SELECT photo_id, photo_name FROM gallery_photos WHERE photo_category = "Books" AND photo_approved = 1 ORDER BY rand()'); while ($row = mysql_fetch_array($query)) { echo "<td><a href='http://www.mysite.com/view.php?pid=".$row['photo_id']."'><img src='photos/".$row['photo_id'].".jpg' width='115' height='115' /><center><h3><b>".$row['photo_name']."</b></h3></center></a></td>"; } ?> </tr> </table> Link to comment https://forums.phpfreaks.com/topic/250550-php-table-loop-issue/ Share on other sites More sharing options...
haku Posted November 6, 2011 Share Posted November 6, 2011 <table cellspacing="10"> <tr> <?php $query = mysql_query('SELECT photo_id, photo_name FROM gallery_photos WHERE photo_category = "Books" AND photo_approved = 1 ORDER BY rand()'); $i = 1; while ($row = mysql_fetch_array($query)) { if($i % 5 === 0) { echo '</tr><tr>'; } echo "<td><a href='http://www.mysite.com/view.php?pid=".$row['photo_id']."'><img src='photos/".$row['photo_id'].".jpg' width='115' height='115' /><center><h3><b>".$row['photo_name']."</b></h3></center></a></td>"; $i++; } ?> </tr> </table> Link to comment https://forums.phpfreaks.com/topic/250550-php-table-loop-issue/#findComment-1285456 Share on other sites More sharing options...
xyph Posted November 6, 2011 Share Posted November 6, 2011 Here's a commented version of the above code using arrays instead of a database result, but the process is the same. <?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>'; ?> Link to comment https://forums.phpfreaks.com/topic/250550-php-table-loop-issue/#findComment-1285601 Share on other sites More sharing options...
jason360 Posted November 6, 2011 Author Share Posted November 6, 2011 Thanks alot guys!!!! Works great!! 8) Link to comment https://forums.phpfreaks.com/topic/250550-php-table-loop-issue/#findComment-1285614 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.