jarvis Posted October 14, 2009 Share Posted October 14, 2009 Hi all, I've the following code <?php // Create category options $query = "SELECT * FROM categories ORDER BY category ASC"; $result = mysql_query ($query); while ($row = mysql_fetch_array ($result, MYSQL_NUM)) { echo "<input type=\"checkbox\" name=\"types[]\" value=\"$row[1]\">$row[1]\n"; } ?> This lists out up to 15 checkboxes, but can increase or decrease if more are added/removed. At present this simply produces a list. How can I alter the above to produce a nice table with say 5 checkboxes per row? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/177638-simple-php-table-loop/ Share on other sites More sharing options...
cags Posted October 14, 2009 Share Posted October 14, 2009 Something like this should suffice... $column_count = 5; $row_count = ceil(mysql_num_rows($query) / $column_count); echo '<table'; for($r = 0; $r < $row_count; ++$r) { // start row echo '<tr>'; for($c = 0; $c < $column_count; ++$c) { if(mysql_fetch_array($result, MYSQL_NUM)) { echo '<td><input type="checkbox" name="types[]" value="' . $row[1] . '" />' .$row[1] . '</td>'; } else { echo '<td> </td>'; } } // end row echo '</tr>'; } echo '</table>'; From a semantics standpoint you probably should replace the table with something else such as divs, I just figured this way was perhaps more understandable. Quote Link to comment https://forums.phpfreaks.com/topic/177638-simple-php-table-loop/#findComment-936641 Share on other sites More sharing options...
jarvis Posted October 14, 2009 Author Share Posted October 14, 2009 Thanks cags, I can't seem to get that working though: <?php // Create category options $query = "SELECT * FROM categories ORDER BY category ASC"; $column_count = 5; $row_count = ceil(mysql_num_rows($query) / $column_count); echo '<table'; for($r = 0; $r < $row_count; ++$r) { // start row echo '<tr>'; for($c = 0; $c < $column_count; ++$c) { if(mysql_fetch_array($result, MYSQL_NUM)) { echo '<td><input type="checkbox" name="types[]" value="' . $row[1] . '" />' .$row[1] . '</td>'; } else { echo '<td> </td>'; } } // end row echo '</tr>'; } echo '</table>'; ?> Gives the error: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Program Files\Apache Group\Apache2\htdocs\lemon_ribbon\web\register.php on line 159 Line 159: $row_count = ceil(mysql_num_rows($query) / $column_count); Sorry to sound thick! Quote Link to comment https://forums.phpfreaks.com/topic/177638-simple-php-table-loop/#findComment-936679 Share on other sites More sharing options...
cags Posted October 14, 2009 Share Posted October 14, 2009 That's my bad, a typo. But you will need to learn to spot things like this for yourself. The value passed to mysql_num_rows should be a query result, whereas I passed it the query string by mistake. So you need to switch the variable being passed. $row_count = ceil(mysql_num_rows($result) / $column_count); Quote Link to comment https://forums.phpfreaks.com/topic/177638-simple-php-table-loop/#findComment-936684 Share on other sites More sharing options...
jarvis Posted October 14, 2009 Author Share Posted October 14, 2009 Thanks cags & apologies! Will try & get that working Quote Link to comment https://forums.phpfreaks.com/topic/177638-simple-php-table-loop/#findComment-936700 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.