Jump to content

Selecting from MySQL populated checkbox list?


manuelV

Recommended Posts

Hi, basically, here's the deal:

I have a lit of checkboxes that are added by the admin (there's an unlimited amount, just depends on how many are added). Then, those are put in a form, in which the user picks whichever ones need to be chosen and those values get sent to a MySQL table.

 

Here's the code that displays the checkboxes

 

<?php
$sql="SELECT * FROM category ORDER BY categoryID ASC";
$result=mysql_query($sql); 

while($row=mysql_fetch_array($result)){
    echo "<input type='checkbox' id='". $row['categoryName'] ."' name='licensed[]' value='" . $row['categoryID'] ."' /><label for='". $row['categoryName'] ."'><br>" . $row['categoryName'] ."</label><br>";
}	
?>	

 

What I'm making now, is an edit form where whichever checkboxes were checked, will show up checked in the edit form. But I'm not really sure how to go about this, since there is only one actual input tag in the code, I can't select the different ones.

 

I also have this SQL query which selects whichever boxes were inputted into the DB

 

$sql2="SELECT * FROM category, categoryInfo WHERE category.categoryID = categoryInfo.categoryID AND categoryInfo.parentID = $parentID";
$result2=mysql_query($sql2); 

 

Where $parentID is the ID of whichever form you're editing.

 

But yes, I'm basically not really sure how to go about this and would like some help figuring this out :)

 

Thanks for your time

Link to comment
Share on other sites

It's fairly simple... when you receive the checkboxes to begin with, I assume you store whether they're checked or not with their ID number... so when you go back to output them, do something like this:

 

$query = mysql_query(" SELECT * FROM categories WHERE something = 'something' ORDER BY whatever ASC") or die(mysql_error());

while($row = mysql_fetch_array($query)) { 

   if($row['checked'] == 1) $checked = "checked";

   echo "<input type='checkbox' name='whatever' id='whatever' ".$checked." >";

}

 

So basically if that ID has the 1 value, or whatever you want in the checked column, it'll include the "checked" in the input tag and it'll be checked on output.

Link to comment
Share on other sites

It's fairly simple... when you receive the checkboxes to begin with, I assume you store whether they're checked or not with their ID number... so when you go back to output them, do something like this:

The way it works is, say we there's the boxes with ID from 1-7. And they choose 2,4,5. It would then insert 3 rows into the table, with the index ID being 'auto increment', but the other ID being 2,4,6.

 

So the method you suggested wouldn't work in this case.

Link to comment
Share on other sites

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.