Jump to content

Archived

This topic is now archived and is closed to further replies.

Julian

How To Take An Array From Your Database And Turn It Into A Bunch Of Checkboxes

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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\">";
}


Share this post


Link to post
Share on other sites
[!--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 conection
or 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!

Share this post


Link to post
Share on other sites
Also I noticed that when I retrived the form back, only the first item of the array is checked, even if I checked various.

Share this post


Link to post
Share on other sites
are you checking the $_POST or $_GET variable for this info? your multiple checkboxes should be represented in $_POST['Features'] or $_GET['Features'] array...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)....

Share this post


Link to post
Share on other sites
Thanks for the help thepip3r.

I have three different files: add.php, list.php and edit.php

First 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

Share this post


Link to post
Share on other sites
Hi, guys, this is killing me, does anybody knows to do this?  Thanks

Share this post


Link to post
Share on other sites
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_checked

Then 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.

Share this post


Link to post
Share on other sites

×

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.