0

CONTEXT

I am developing a profile creation form with a checkbox group that implodes and inserts the comma-separated array into a MySQL table ($profile_table).

<input type="checkbox" name="check_group[]" value="Foo" /> Foo<br />
<input type="checkbox" name="check_group[]" value="Bar" /> Bar<br /> 
<input type="checkbox" name="check_group[]" value="Baz" /> Baz<br />
                
$insert_array = implode(", ", $_POST['check_group']);
               
               

If all checkboxes are checked, the array looks like: "Foo, Bar, Baz"

$insert_profile_query = "INSERT INTO profile_table
(check_group_field)VALUES ('".$insert_array."')";   
$profile_query_qs = db_query($insert_profile_query);
               

The code above works.

I need an edit profile page where the checkbox group is pre-filled using the array that was created above and inserted in the MySQL $profile_table.

Using the "Foo" checkbox as an example, I believe the answer uses an in_array conditional and is something close to:

<input type="checkbox" name="check_group[]" value="Foo"
<? $my_array = array(explode(", ", $profile_table['check_group_field'])); 
if (in_array("Foo", $my_array)) {echo 'checked="checked"';}?> /> Foo<br />

QUESTION

Specifically, what is the best function to get the array out of the MySQL $profile_table.check_group_field so I can use it in the in_array conditional as $my_array to pre-fill the checkboxes in the checkbox group? Do I need to explode the array first?

EXAMPLE RESOURCES

Here is an example of the many resources I am referencing; but, I am not finding the specific issue I need to address:

https://www.php.net/in_array http://www.sitepoint.com/forums/showthread.php?577188-Retrieving-A-Checkbox-Array-from-MySql Populate checkbox from array

Thanks in advance!

3 Answers 3

2
$x=mysql_query("SELECT * FROM DB");
while($y=mysql_fetch_array){ 
echo "<input type='checkbox' name=".$y." value='Foo' >";
}
Sign up to request clarification or add additional context in comments.

Comments

0

I would store the values of the checkboxes in an array and run through all of the options in a foreach and check if the value is in the array in a similar fashion to what you used.

$options=array('Foo', 'Bar', 'Baz');
$my_array = array(explode(", ", $profile_table['check_group_field']));
foreach($options as $option){
    echo '<input type="checkbox" name="check_group[]" value="'.$option.'" ';
    if(in_array($option, $my_array)){
        echo 'checked="checked"';
    }
    echo '/>';
}

Comments

0

Thank you for confirming I was in the ballpark! I played around with the function more and by deduction found my error.

$my_array = array(explode(", ", $profile_table['check_group_field'])); is wrong.

Explode creates an array, so array(explode... is redundant.

I believe the statement should be:

$my_array = (explode(", ", $profile_table['check_group_field']));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.