Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/177638-simple-php-table-loop/
Share on other sites

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>&nbsp</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.

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>&nbsp</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!

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);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.