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; } ?> 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? 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. 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"; 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. 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 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"; 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
Archived
This topic is now archived and is closed to further replies.