tHud Posted September 5, 2009 Share Posted September 5, 2009 (I am a total php newbie) Hello I would like to output the content of a one column mysql table into a grid - sort of like this... apple pear cherry banana kiwi orange mango peach plum as apple | pear | cherry | banana kiwi | orange | mango | peach plum | etc etc so I'm thinking... while($row = mysql_fetch_row($result)) { create table code here } I tried the following to create a set number of columns and rows... for ($cols=0; $cols < 4; $cols++) { echo "<tr>"; for ($rows=0; $rows < 6; $rows++) { echo "<td> </td>"; } echo "</tr>"; } echo '</table>'; My problem is, how do I enter the result $row[0] in between the <TD> tags? Or how do I get to 'break out' of the loop to get the next $row[0] result. As it is - the same record is being printed over and over again? I'm very confused Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/173258-outputting-mysql-as-an-html-grid/ Share on other sites More sharing options...
wildteen88 Posted September 5, 2009 Share Posted September 5, 2009 This FAQ Post will help you out. Quote Link to comment https://forums.phpfreaks.com/topic/173258-outputting-mysql-as-an-html-grid/#findComment-913271 Share on other sites More sharing options...
tHud Posted September 6, 2009 Author Share Posted September 6, 2009 Thanks a million There seems to be an issue with that code. For the most part is does what I want, but if the number of results from the MySQL table is an exact multiple of $max_columns - then this bit kicks in... if($i < $max_columns) { for($j=$i; $j<$max_columns;$j++) echo "<td> </td>"; } ...and you get superfluous data cells added. <td> </td><td> </td><td> </td></tr> I know this post would be better off in the 'actual' topic - but it seems I am not allowed to post there. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/173258-outputting-mysql-as-an-html-grid/#findComment-913657 Share on other sites More sharing options...
tHud Posted September 7, 2009 Author Share Posted September 7, 2009 I changed the code at the end of the table to this... } // end if if ($i != 0) if($i < $max_columns) { for($j=$i; $j<$max_columns;$j++) { echo "<td> </td>";} echo "</tr>"; } ?> </table> It's now working whether or not the number of rows returned is the same as $max_columns As newbie code goes, I'm sure that's a sloppy solution, so if anyone can suggest something better - I'd be happy to listen Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/173258-outputting-mysql-as-an-html-grid/#findComment-914242 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.