mahalleday Posted February 9, 2009 Share Posted February 9, 2009 What i need to do is loop through an array that i take from a table in my database create/populate a table with its contents similar to this.. array[0] array[1] array[3] array[4] array[5] array[6] array[7] array[8] array[9] here is the code i have so far but i can't get the result i need. <?php $j = 1; while($cats = $cat_query->fetchRow()) { echo '<tr>'; while($j <= $cols) { echo'<td><p><img src="" /><br/>'.$cats['cat_title'].'<br/>'.$cats['cat_desc'].'</p></td>'; $j++; } $j = 1; echo '</tr>'; } ?> I can make this work if it an array i created with php it self ie: $items = array("item1", "item2") and the referencing their positions in the array (0, 1, 2 etc...), but not when i create the array from a mysql table. Link to comment https://forums.phpfreaks.com/topic/144438-solved-looping-thru-an-array-to-populate-a-table/ Share on other sites More sharing options...
flyhoney Posted February 9, 2009 Share Posted February 9, 2009 Try this: <table> <tr> <?php $column = 0; $columns = 3; while ($cats = $cat_query->fetchRow()) { if ($column and !($column % $columns)) { echo '</tr><tr>'; } echo '<td><p><img src="" /><br/>'.$cats['cat_title'].'<br/>'.$cats['cat_desc'].'</p></td>'; $column++; } for ($x = $column; $x < $columns; $x++) { echo '<td> </td>'; } ?> </tr> </table> Link to comment https://forums.phpfreaks.com/topic/144438-solved-looping-thru-an-array-to-populate-a-table/#findComment-757944 Share on other sites More sharing options...
mahalleday Posted February 10, 2009 Author Share Posted February 10, 2009 That seemed to do the trick thanks a lot. Link to comment https://forums.phpfreaks.com/topic/144438-solved-looping-thru-an-array-to-populate-a-table/#findComment-758590 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.