Forum Moderators: coopster

Message Too Old, No Replies

returning checked boxes from an array

returning checked values from a database

         

knotworking

2:53 am on Dec 18, 2004 (gmt 0)

10+ Year Member



I don't know if this is possible but, I'm hoping it is.

I have a field of checkboxes (12 or so) that I then submit into a mysql Db SET via implode for each value that has been checked. Works great.

Here's my sticking point. These submitted checkboxes are part of a user profile. I want the user to be able to see the boxes that they have checked and be able to uncheck or check new boxes in the field. Can't figure out how to do this.

Is it possible to return values from a database into selected check boxes? How would I call them? Do I need to format the data or seperate it into different columns? Right now the are all being stored in one field seperated by commas.

Hope this makes sense, I appreciate any feedback or advice!

Salsa

4:42 am on Dec 18, 2004 (gmt 0)

10+ Year Member



Not only is it possible, but it is fairly common. Suppose in your database you represent that a checkbox is checked with 1, and if not, 0. Then, after you SELECT the data to re-present the form, you might do something like:

if ($datum == 1) $checked = " checked";
else $checked = "";
echo "<input type=\"checkbox\" name=\"datum\" value=\"1\"$checked>\n";

jatar_k

4:42 am on Dec 18, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you can select them and then split/explode them

then whne you come to each checkbox in question. If that value exists in the db, then

<?php echo " checked";?>

inside the checkbox so that it will be selected and they can edit the data if need be.

<added>Salsa beat me ;)

Salsa

5:07 am on Dec 18, 2004 (gmt 0)

10+ Year Member



But Jatar! You messed up my opportunity to edit (cleanly). I thought to add:

You can reduce this to two lines with the ternary operator [php.net].

jatar_k

5:28 am on Dec 18, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hehe ;)

Salsa

5:39 am on Dec 18, 2004 (gmt 0)

10+ Year Member



The worse news is that the ternary link that I offered is no longer good! At the time, php.net was apparently down, and I was unable to check it. But here's an example from another page [us4.php.net].

knotworking

7:47 am on Dec 19, 2004 (gmt 0)

10+ Year Member



Sorry for the delayed response, traveling across the US for the holidays. Bad time of year to fly.

Thanks for the leads, I'll try implementing this into my script. I'm not quite clear on everything but, tinkering should help me get it.

Happy Holidays

knotworking

4:29 pm on Dec 20, 2004 (gmt 0)

10+ Year Member



I've tinkered for two days and, I am stuck. I was able to get one checkbox to return checked with this formula:

if ($experience == "National Tour") $national = "checked";
else $national = "";

<?echo "<input type=\"checkbox\" name=\"experiences[]\" value=\"National Tour\"$national>";?>

But that was as far I can get. Here's what I'm trying to work with:

-my checkboxes are all named "experiences[]" creating a SET array ($experiences).

-I submit the checked $experiences with this code:

if ($experiences == ""){$experience = "";
} else {
$experience = ''.implode(',',$experiences).',';
}

And that's where I am. My field strings are seperated by ',' in the Db, that I tried to explode but, I could only get the explode to echo 'array'(the explaination of explode was simple enough but, I haven't used it before). I'm thinking that it would have been easier to have seperate Db fields for each checkbox. I tried this with no luck:

if ($experience == "Regional Tour") $regional = "checked";
else $regional = "";
if ($experience == "Market Promotion") $market = "checked";
else $market = "";
if ($experience == "MC-Announcing Experience") $mc = "checked";
else $mc = "";
//etc

If I could get the field string to explode into strings that match up with the above I would be OK, right? I'm kinda lost at this point. Can you guys give me another few lines in the right direction?

Salsa

6:53 pm on Dec 20, 2004 (gmt 0)

10+ Year Member



Yes, simply echoing an array won't tell you much. To see what's in your array, try:

echo "<pre>";
print_r($experiences);
echo "</pre>";
Now, when you write your form fields, if you first have all of your checkbox values in an array, like:

$experiences = array('National Tour', 'Regional Tour',...);
...you can save yourself a lot of typing if you do something like this:

for ($i = 0; $i < count($experiences); $i++){
echo "$experiences[$i]: <input type=\"checkbox\" name=\"experiences[$i]\" value=\"$experiences[$i]\"><br>\n";
}
That should write the entire block of checkboxes for you. Tack on whatever formatting you want, with <p>'s and CSS or table tags, etc.

But what about checking the appropriate check boxes? When you query experience from the database, you could put them in an array like this

$saved_experiences = explode(',',$query_data['experience'];
...then edit the above code to:

for ($i = 0; $i < count($experiences); $i++){
[b]$checked = in_array($experiences[$i],$saved_experiences)? " checked" : "";[/b]
echo "$experiences[$i]: <input type=\"checkbox\" name=\"experiences[$i]\" value=\"$experiences[$i]\"[b]$checked>[/b]<br>\n";
}
I haven't tested this, so typos are not unlikely.

I hope this helps.

knotworking

10:38 pm on Dec 20, 2004 (gmt 0)

10+ Year Member



I'll give it a whirl, thanks for the code (I can handle typos, script is where I get lost)!

knotworking

8:37 pm on Dec 21, 2004 (gmt 0)

10+ Year Member



I've got the array generating dynamically in the form (I spent the last day trying to figure out how to get the table rows and columns to line up nicely). But, still no dice on returning the checked values from the database. Here's what I've got:

$experiences = array('National Tour', 'Regional Tour', ...)

$cols=3;
$cells=0;
$td='';
for ($i = 0; $i < count($experiences); $i++){
$cells++;
$saved_experiences = explode(',',$query_data['experience']);
$checked = in_array($experiences[$i],$saved_experiences)? "checked" : "";
$td.= "\n\t<td width= \"33%\" align=\"left\">&nbsp;<input type=\"checkbox\" name=\"experiences[$i]\" value=\"$experiences[$i]\"$checked> $experiences[$i]</td>";
if($cells==$cols)
{ echo "\n<tr> $td \n</tr>";
$cells=0;
$td="";
}
}

Could it possibly be that my string seperator ',' is not the leading character in my SET array (National Tour,Regional Tour,..). I don't think so, I tried to lead with it by adding that into my implode but, no dice on getting the SET to lead with a ','. That's probably not the reason, in any case.

Sorry this is dragging on. I've got all the rest of my form finalized, this is the last bit and, it been a bugger. I appreciate the help!

Salsa

10:58 pm on Dec 21, 2004 (gmt 0)

10+ Year Member



...just a couple of questions to clarify if we're communicating. Are you properly completing

$experiences = array('National Tour', 'Regional Tour', ...)

...with all of the values? My '...' was just shorthand to add in the rest of the array elements. From what you said, I think you have, but just wanted to make certain.

Then, the

$saved_experiences = explode(',',$query_data['experience']);

...line, assumes that you have connected to the database and done a query like:

$result = mysql_query("SELECT experience FROM table WHERE userid stuff"); 
$query_data = mysql_fetch_array($result);

You can use any variable name that you wish for $query_data, just so you substitute the same name when you explode $query_data['experience']

As an aside, I would encourage you to consider making your MySQL 'experience' column a SET type [dev.mysql.com] column. This would make the storage and processing of data much more efficient, PLUS you could query the table like:

$result = mysql_query("SHOW COLUMNS FROM table LIKE 'experience'"); 
$query_data = mysql_fetch_array($result);
$experiences = explode("','",substr($query_data['Type'],5,-2));

...which would create your $experiences array, and make it unnecessary to maintain the contents of $experiences and $saved_experiences in two places.

I wish you well.

knotworking

12:38 am on Dec 22, 2004 (gmt 0)

10+ Year Member



Salsa-

Yes, I'm making a connection and selecting all data from my table (I have about 30 fields that are all returning profile data correctly). I use SET for many of my fields, $experience is indeed a SET field already, so that means I don't need to declare it's contents? I'll try stripping it out and seeing if I'm still dynamically creating the checkbox array.

All my other user data is returning properly, just not the selected checkboxes. Could the fact that I'm declaring it in the script and also calling it be the problem?

Salsa

2:35 am on Dec 22, 2004 (gmt 0)

10+ Year Member



Knotworking, I went ahead and ran a test of the code I gave you in msg #9 (a missing close parenthesis in the explode added), and it worked fine. Here's the code I used:

<?php
$experiences = array('National Tour', 'Regional Tour', 'Market Promotion', 'MC-Announcing Experience', 'Something Else');
$saved_experiences = array('National Tour', 'Market Promotion');

for ($i = 0; $i < count($experiences); $i++){ 
$checked = in_array($experiences[$i],$saved_experiences)? " checked" : "";
echo "$experiences[$i]: <input type=\"checkbox\" name=\"experiences[$i]\" value=\"$experiences[$i]\"$checked><br>\n";
}
?>

The idea is to have ALL possible experiences in $experiences, and only the already checked and saved experiences of the particular user in $saved_experiences. When I ran this, all the checkboxes in $experiences displayed with only those in $saved_experiences checked.

I hope this helps.

knotworking

3:30 am on Dec 22, 2004 (gmt 0)

10+ Year Member



I figured it out (FINALLY). I was trying to explode $experience by using

$query_data['experience']

varying query_data with any other variable I could think of. turns out all I needed to do was:

$saved_experiences = explode(',',$experience);

Thanks for all your help, Jatar & Salsa. I'd still be scratching my head wondering where to start without the two of you.

Happy Holidays!