JPark Posted February 13, 2009 Share Posted February 13, 2009 I can't figure out the loop logic on this one and would appreciate your help. Say I have an array: $test = array("first => this one", "second => that one", "third => the other one", ... "twentiest => the other other one"); And I want php to automatically generate a table that is 5 columns wide and 4 rows tall, populating it with the array items, how do I do it? I can get all rows or all columns but not all of both. Help? Please and thank you. Joe Quote Link to comment https://forums.phpfreaks.com/topic/145075-solved-looping-question-creating-a-tale-from-an-array/ Share on other sites More sharing options...
AdRock Posted February 13, 2009 Share Posted February 13, 2009 I don't know if you can get what you want from this. This does 4 columns until it reaches the end define ("NUMCOLS",4); $res = mysql_query("SELECT id,thumb,cat FROM image WHERE cat='events'"); $count = 0; echo "<table>"; while (list($id,$thumb,$cat) = mysql_fetch_row($res)) { if ($count % NUMCOLS == 0) echo "<tr>\n"; # new row echo "<td>stuff in here</td>\n"; $count++; $counter++; if ($count % NUMCOLS == 0) echo "</tr>\n"; # end row } # end row if not already ended if ($count % NUMCOLS != 0) { while ($count++ % NUMCOLS) echo "<td> </td>"; echo "</tr>\n"; } echo "</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/145075-solved-looping-question-creating-a-tale-from-an-array/#findComment-761275 Share on other sites More sharing options...
sasa Posted February 13, 2009 Share Posted February 13, 2009 try <?php $test = range(1,20); $test = array_chunk($test, 5); echo "<table>\n"; foreach ($test as $line) echo "<tr>\n\t<td>", implode("</td>\n\t<td>", $line), "</td>\n</tr>\n"; echo "</table>\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/145075-solved-looping-question-creating-a-tale-from-an-array/#findComment-761310 Share on other sites More sharing options...
JPark Posted February 13, 2009 Author Share Posted February 13, 2009 But what if I wanted to use my array statement instead of querying the database? $test = array("first => this one", "second => that one", "third => the other one", ... "twentiest => the other other one"); instead of $res = mysql_query("SELECT id,thumb,cat FROM image WHERE cat='events'"); Quote Link to comment https://forums.phpfreaks.com/topic/145075-solved-looping-question-creating-a-tale-from-an-array/#findComment-761311 Share on other sites More sharing options...
JPark Posted February 13, 2009 Author Share Posted February 13, 2009 try <?php $test = range(1,20); $test = array_chunk($test, 5); echo "<table>\n"; foreach ($test as $line) echo "<tr>\n\t<td>", implode("</td>\n\t<td>", $line), "</td>\n</tr>\n"; echo "</table>\n"; ?> Sasa, NICE! One question... where/how would I specify MY array $myarray = array("first => this one", "second => that one", "third => the other one", ... "twentiest => the other other one"); Quote Link to comment https://forums.phpfreaks.com/topic/145075-solved-looping-question-creating-a-tale-from-an-array/#findComment-761316 Share on other sites More sharing options...
sasa Posted February 15, 2009 Share Posted February 15, 2009 change line $test = range(1,20); to $test = $myarray; Quote Link to comment https://forums.phpfreaks.com/topic/145075-solved-looping-question-creating-a-tale-from-an-array/#findComment-762598 Share on other sites More sharing options...
JPark Posted February 17, 2009 Author Share Posted February 17, 2009 Sasa, Now THAT is cool!!! Much thanks! Joe Quote Link to comment https://forums.phpfreaks.com/topic/145075-solved-looping-question-creating-a-tale-from-an-array/#findComment-764417 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.