Jump to content

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.
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\">";
}


[!--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!

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