samtwilliams Posted February 2, 2013 Share Posted February 2, 2013 I am trying to draw a table dynamically, I need a table that is 5 x 5, i know i'm close quite my code by just can't quite fathom it out. My sql returns 25 results which i want to return 25 cells in a square table. <?PHP $i = 0; while($row = mysql_fetch_assoc($result)) { $i++; ?> <?PHP if($i== 5) { echo '<tr>'; } ?> <td class="right bottom"><?PHP echo $row['GridID']; ?></td> <td class="right bottom"> </td> <td class="right bottom"> </td> <td class="right bottom"> </td> <td class="bottom"> </td> <?PHP if($i== 5) { echo '</tr>'; $i=0; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/273954-create-a-new-row-every-when-i-5/ Share on other sites More sharing options...
razorsese Posted February 2, 2013 Share Posted February 2, 2013 Have you tried replacing the statement if($i == 5) with if($i % 5 ==0 ) so at every 5 item its creating a new tr? Quote Link to comment https://forums.phpfreaks.com/topic/273954-create-a-new-row-every-when-i-5/#findComment-1409735 Share on other sites More sharing options...
Jessica Posted February 2, 2013 Share Posted February 2, 2013 You need to end the row BEFORE starting a new one. You're ending the new row you just created. Quote Link to comment https://forums.phpfreaks.com/topic/273954-create-a-new-row-every-when-i-5/#findComment-1409736 Share on other sites More sharing options...
Barand Posted February 2, 2013 Share Posted February 2, 2013 or use floating divs, 20% wide echo "<div style='width:200px'>\n"; for ($i=1; $i<=25; $i++) { echo "<div style='width:20%; float:left; text-align:center; border:1px solid gray;'> $i </div>"; } echo "</div>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/273954-create-a-new-row-every-when-i-5/#findComment-1409739 Share on other sites More sharing options...
samtwilliams Posted February 2, 2013 Author Share Posted February 2, 2013 I need to use tables, still can't get it to work, my table just looks a mess. Quote Link to comment https://forums.phpfreaks.com/topic/273954-create-a-new-row-every-when-i-5/#findComment-1409740 Share on other sites More sharing options...
Barand Posted February 2, 2013 Share Posted February 2, 2013 (edited) echo "<table border='1' style='width:200px; border-collapse:collapse;'>\n"; echo "<tr>"; for ($i=1; $i<=25; $i++) { echo "<td style='text-align:center; '> $i </td>"; if ($i%5==0) echo "</tr><tr>"; } echo "</tr></table>\n"; Edited February 2, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/273954-create-a-new-row-every-when-i-5/#findComment-1409744 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.