thisagain Posted July 19, 2006 Share Posted July 19, 2006 Hello. I'm new to this forum and new to PHP so please forgive me for my amature code.I am trying to generate a 4 column table that is filled with data from a database. I have the following code which allows me to fill the table with one row and infinite columns (until I have no more data). I want to close every row at 4 columns and start a new row and continue showing the data until I have no more data.[hr]<table> <tr><?phpif($results){ while ($row = mysql_fetch_array($results)){ $key = $row["key"]; $name = $row["name"]; $photo = $row["photo"];?> <!-- <?php echo($key); ?> --> <td> <a href="items.php?name=<?php echo("name"); ?>"><img src="<?php echo($photo); ?>" height="150" width="100"><br><?php echo($name); ?></a> </td><?php }}?> </tr></table>[hr]Am I using the wrong type of loop? I haven't been able to put a count in the loop that actually increases every time it loops. I hope someone can help me with this. Would really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/15067-help-closing-table-row-in-a-while-loop/ Share on other sites More sharing options...
akitchin Posted July 19, 2006 Share Posted July 19, 2006 you can do the following, although you may have to fine-tune:[code]<?php$per_row = 4;$i = 1;while (stuff){ // end the last row and start a new row if we're on a new set if (($i % $per_row) == 1 && $i > 1) { echo '</tr><tr>'; } // echo a cell // increment the cell counter $i++;}// find the requisite number of empty cells$empties = $per_row - ($i % $per_row);// echo the empty cell and end the rowecho '<td colspan="'.$empties.'"> </td></tr>';?>[/code]hth Quote Link to comment https://forums.phpfreaks.com/topic/15067-help-closing-table-row-in-a-while-loop/#findComment-60594 Share on other sites More sharing options...
thisagain Posted July 19, 2006 Author Share Posted July 19, 2006 Thanks akitchin! I really appreciate the help.Everything works except $empties gives me the wrong amount but I can figure that out. Quote Link to comment https://forums.phpfreaks.com/topic/15067-help-closing-table-row-in-a-while-loop/#findComment-60663 Share on other sites More sharing options...
akitchin Posted July 20, 2006 Share Posted July 20, 2006 sorry, it should be:[code]$empties = $per_row - (--$i % $per_row);[/code]since the $i represents the next cell number, not the one that has just executed. Quote Link to comment https://forums.phpfreaks.com/topic/15067-help-closing-table-row-in-a-while-loop/#findComment-60849 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.