Jump to content


Photo

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


  • Please log in to reply
9 replies to this topic

#1 Julian

Julian
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts

Posted 29 June 2006 - 04:57 PM

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.

#2 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 29 June 2006 - 05:53 PM

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



Life's too short for arguing.

#3 Julian

Julian
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts

Posted 29 June 2006 - 06:17 PM

[!--quoteo(post=389365:date=Jun 29 2006, 11:53 AM:name=hvle)--][div class=\'quotetop\']QUOTE(hvle @ Jun 29 2006, 11:53 AM) View Post[/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!



#4 Julian

Julian
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts

Posted 29 June 2006 - 07:04 PM

Also I noticed that when I retrived the form back, only the first item of the array is checked, even if I checked various.

#5 thepip3r

thepip3r
  • Members
  • PipPipPip
  • Advanced Member
  • 289 posts

Posted 29 June 2006 - 08:20 PM

are you checking the $_POST or $_GET variable for this info? your multiple checkboxes should be represented in $_POST['Features'] or $_GET['Features'] array...

#6 Julian

Julian
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts

Posted 29 June 2006 - 08:54 PM

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.

#7 thepip3r

thepip3r
  • Members
  • PipPipPip
  • Advanced Member
  • 289 posts

Posted 29 June 2006 - 08:59 PM

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

#8 Julian

Julian
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts

Posted 29 June 2006 - 09:12 PM

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

#9 Julian

Julian
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts

Posted 30 June 2006 - 03:39 PM

Hi, guys, this is killing me, does anybody knows to do this?  Thanks

#10 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 30 June 2006 - 03:49 PM

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...
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;
}
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.
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users