Julian Posted June 29, 2006 Share Posted June 29, 2006 Hi guys.Here I am again.First I have a form where I insert the info into a database. In this form I have a bunch of checkboxes and I store them into the database using: $varnew = implode(", ", $_POST['Features']);This worked great. I could store the info as an array like: (Mexican, Italian, Thai), used Features[] on the checkbox: <input type="checkbox" name="Features[]" value="Mexican">....What I'm trying to do is another editable form that gets the info from a 'Features' table into a "Checked" checkboxes, like if I have Mexican already stored on the table, I want it to be a checked checkbox when I retrieve the form.Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/13227-how-to-take-an-array-from-your-database-and-turn-it-into-a-bunch-of-checkboxes/ Share on other sites More sharing options...
hvle Posted June 29, 2006 Share Posted June 29, 2006 you have:$all_features = array(list all the feature you have here);say you retrived the feature have:$choices = 'Thai, Mexican'; // just a sample$arr = explode(',', $choices);foreach ($all_features as $feature){ if (in_array($feature, $arr) echo "<input type=\"checkbox\" name=\"Features[]\" value=\"$feature\" checked>"; else echo "<input type=\"checkbox\" name=\"Features[]\" value=\"$feature\">";} Quote Link to comment https://forums.phpfreaks.com/topic/13227-how-to-take-an-array-from-your-database-and-turn-it-into-a-bunch-of-checkboxes/#findComment-50930 Share on other sites More sharing options...
Julian Posted June 29, 2006 Author Share Posted June 29, 2006 [!--quoteo(post=389365:date=Jun 29 2006, 11:53 AM:name=hvle)--][div class=\'quotetop\']QUOTE(hvle @ Jun 29 2006, 11:53 AM) [snapback]389365[/snapback][/div][div class=\'quotemain\'][!--quotec--]you have:$all_features = array(list all the feature you have here);say you retrived the feature have:$choices = 'Thai, Mexican'; // just a sample$arr = explode(',', $choices);foreach ($all_features as $feature){ if (in_array($feature, $arr) echo "<input type=\"checkbox\" name=\"Features[]\" value=\"$feature\" checked>"; else echo "<input type=\"checkbox\" name=\"Features[]\" value=\"$feature\">";}[/quote]Thanks hvle, awesome help. I have a question (maybe silly).Can I make $all_features = $row_edit['Features']; //$row_edit is my sql conectionor I have to list all the options including those that are not checked? or I put this on $choices = $row_edit['Features'];Thank you again! Quote Link to comment https://forums.phpfreaks.com/topic/13227-how-to-take-an-array-from-your-database-and-turn-it-into-a-bunch-of-checkboxes/#findComment-50938 Share on other sites More sharing options...
Julian Posted June 29, 2006 Author Share Posted June 29, 2006 Also I noticed that when I retrived the form back, only the first item of the array is checked, even if I checked various. Quote Link to comment https://forums.phpfreaks.com/topic/13227-how-to-take-an-array-from-your-database-and-turn-it-into-a-bunch-of-checkboxes/#findComment-50960 Share on other sites More sharing options...
thepip3r Posted June 29, 2006 Share Posted June 29, 2006 are you checking the $_POST or $_GET variable for this info? your multiple checkboxes should be represented in $_POST['Features'] or $_GET['Features'] array... Quote Link to comment https://forums.phpfreaks.com/topic/13227-how-to-take-an-array-from-your-database-and-turn-it-into-a-bunch-of-checkboxes/#findComment-50989 Share on other sites More sharing options...
Julian Posted June 29, 2006 Author Share Posted June 29, 2006 Hi. I don't understand the question.... silly me... [img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /] I don't have problem saving the array Features[]. The values of Features[] are already saved on the Features table on my database. I'm trying to get them back as checked checkboxes. Quote Link to comment https://forums.phpfreaks.com/topic/13227-how-to-take-an-array-from-your-database-and-turn-it-into-a-bunch-of-checkboxes/#findComment-51012 Share on other sites More sharing options...
thepip3r Posted June 29, 2006 Share Posted June 29, 2006 Ok, your checkboxes have to be enclosed in an HTML <form> tag right? well that form has a METHOD parameter that you need to set if you haven't (<form method='POST'> or <form method='GET'>) your <form> tag also needs and ACTION parameter and so when your SUBMIT button is pressed, your form knows which super global it needs to load all of your form variables to (ie the $_POST or $_GET superglobals).... Quote Link to comment https://forums.phpfreaks.com/topic/13227-how-to-take-an-array-from-your-database-and-turn-it-into-a-bunch-of-checkboxes/#findComment-51013 Share on other sites More sharing options...
Julian Posted June 29, 2006 Author Share Posted June 29, 2006 Thanks for the help thepip3r.I have three different files: add.php, list.php and edit.phpFirst I make a new record, including the checkboxes using add.php via $_POST, it works fine, including the info on the database as an array I used: $varnew = implode(", ", $_POST['Features']);Second I want to update the database, on the edit.php I want to retrieve the previously checked checkboxes in order to maintain or update the data.I hope this explain myself. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/13227-how-to-take-an-array-from-your-database-and-turn-it-into-a-bunch-of-checkboxes/#findComment-51021 Share on other sites More sharing options...
Julian Posted June 30, 2006 Author Share Posted June 30, 2006 Hi, guys, this is killing me, does anybody knows to do this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/13227-how-to-take-an-array-from-your-database-and-turn-it-into-a-bunch-of-checkboxes/#findComment-51287 Share on other sites More sharing options...
Buyocat Posted June 30, 2006 Share Posted June 30, 2006 Jul, you're trying to pull info from a database and construct a series of checkboxes, checked and unchecked, based on that data?I'll leave most of the details to you but I would have a table something like this...Columns:box_id : box_name : box_value : box_checkedThen I would query the table and with the resulting array do the following...[code]foreach ($results as $checkbox){// I forget some of the HTML, so correct it!$html_box = '<input type="checkbox" name="' . $checkbox['name'] . '" value="' . $checkbox['value'] . '" checked="' . $checkbox['status'] . '" />';echo $html_box;}[/code]I recognize that the above code will need to change quite a bit to accommodate whatever you're doing, but I hope you can see what I'm accomplishing with it and bend it to your needs. Quote Link to comment https://forums.phpfreaks.com/topic/13227-how-to-take-an-array-from-your-database-and-turn-it-into-a-bunch-of-checkboxes/#findComment-51290 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.