Forum Moderators: coopster
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!
if ($datum == 1) $checked = " checked";
else $checked = "";
echo "<input type=\"checkbox\" name=\"datum\" value=\"1\"$checked>\n";
You can reduce this to two lines with the ternary operator [php.net].
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?
Now, when you write your form fields, if you first have all of your checkbox values in an array, like: echo "<pre>";
print_r($experiences);
echo "</pre>";
...you can save yourself a lot of typing if you do something like this: $experiences = array('National Tour', 'Regional Tour',...);
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. for ($i = 0; $i < count($experiences); $i++){
echo "$experiences[$i]: <input type=\"checkbox\" name=\"experiences[$i]\" value=\"$experiences[$i]\"><br>\n";
}
But what about checking the appropriate check boxes? When you query experience from the database, you could put them in an array like this
...then edit the above code to: $saved_experiences = explode(',',$query_data['experience'];
I haven't tested this, so typos are not unlikely. 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 hope this helps.
$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\"> <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!
$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.
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?
<?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.
$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!