manuelV Posted August 24, 2011 Share Posted August 24, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/245627-selecting-from-mysql-populated-checkbox-list/ Share on other sites More sharing options...
msaz87 Posted August 24, 2011 Share Posted August 24, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/245627-selecting-from-mysql-populated-checkbox-list/#findComment-1261586 Share on other sites More sharing options...
manuelV Posted August 24, 2011 Author Share Posted August 24, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/245627-selecting-from-mysql-populated-checkbox-list/#findComment-1261594 Share on other sites More sharing options...
msaz87 Posted August 24, 2011 Share Posted August 24, 2011 You could put the checked IDs into an array before you output the same list of checkboxes, then have something that compares the ID of that checkbox in the loop to see if it's in the array and if it is, to echo the "checked" bit Quote Link to comment https://forums.phpfreaks.com/topic/245627-selecting-from-mysql-populated-checkbox-list/#findComment-1261608 Share on other sites More sharing options...
manuelV Posted August 25, 2011 Author Share Posted August 25, 2011 Right, I'm just not exactly how to go about that in code form.. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/245627-selecting-from-mysql-populated-checkbox-list/#findComment-1261660 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.