Retrieving A Checkbox Array from MySql

I have a registration form with a checkbox section that is created by an array.



<?php	
foreach ($cityarray as $key => $value)
{

echo '
<input type="checkbox" name="cityselection[] "id="cityselection[]" value="'.$value.'"/><label for="cityselection[]">'.$value.'</label><br />';
}
?>


So these are just city names and the Name of each city is = to the Value attribute of each checkbox. … e.g. it looks like this when it is executed…



<input type="checkbox" value="New York" /><label>New York</label>


A user can select as many cities as he would like and upon submit the checbox array is imploded into a comma separated string and that string is stored in one field.

If I want to build an update form to change the original selections … How do I pull these values, explode them and pre-check the checkbox list so it will reflect those values so I can start making changes …

thanks in advance for any help.

By INSERTing your new data INTO mysql table and SELECTing the recodes with GROUP BY,ORDER BY

Ok I just checked into this and it looks like I have to pull the data string … explode it … and compare the exploded array against my checkbox section to pre-check any of the checkboxes that would apply does this make any sense to you … I feel like I am almost there … or maybe I am delusional …



// EXPLODE THE STRING
$getcityarray = htmlspecialchars(stripslashes($business['cityarray']));
$explcitydarray = explode(",", $getcityarray);


//BUILD THE DYNAMIC CHECKBOX LISY

<?php	
foreach ($cityarray as $key => $value) ------- // THIS ARRAY JUST PULLS ALL THE CITIES
{
echo '
<input type="checkbox" name="cityselection[] "id="cityselection[]"  value="'.$value.'"
if (isset($explcitydarray) && in_array('.$value.', $explcitydarray)) {
checked="checked"}
/><label for="cityselection[]">'.$value.'</label><br />';
}
?>


I know I am missing some stuff but am I headed in the right direction?

thanks in advance for your help.

This query ended up working … thanks!

<?php

foreach ($cityarray as $key => $value)
{?>

<input type=“checkbox” name=“cityselection " id=“cityselection” value=”<?php echo “$value” ;?>"
<?php if (isset($explcitydarray) && in_array(“$value” , $explcitydarray))
{echo ‘checked=“checked”’ ; }?>
/>
<label for=“cityselection”><?php echo “$value” ; ?></label><br />

<?php } ?>