Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing PHP, Retrieve Array from MySQL Field and Use It to Fill Checkbox Group
    primarykey
    data
    text
    <p><strong>CONTEXT</strong></p> <p>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).</p> <pre><code>&lt;input type="checkbox" name="check_group[]" value="Foo" /&gt; Foo&lt;br /&gt; &lt;input type="checkbox" name="check_group[]" value="Bar" /&gt; Bar&lt;br /&gt; &lt;input type="checkbox" name="check_group[]" value="Baz" /&gt; Baz&lt;br /&gt; $insert_array = implode(", ", $_POST['check_group']); </code></pre> <p>If all checkboxes are checked, the array looks like: "Foo, Bar, Baz"</p> <pre><code>$insert_profile_query = "INSERT INTO profile_table (check_group_field)VALUES ('".$insert_array."')"; $profile_query_qs = db_query($insert_profile_query); </code></pre> <p>The code above works.</p> <p>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.</p> <p>Using the "Foo" checkbox as an example, I believe the answer uses an in_array conditional and is something close to: </p> <pre><code>&lt;input type="checkbox" name="check_group[]" value="Foo" &lt;? $my_array = array(explode(", ", $profile_table['check_group_field'])); if (in_array("Foo", $my_array)) {echo 'checked="checked"';}?&gt; /&gt; Foo&lt;br /&gt; </code></pre> <p><strong>QUESTION</strong></p> <p>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?</p> <p><strong>EXAMPLE RESOURCES</strong></p> <p>Here is an example of the many resources I am referencing; but, I am not finding the specific issue I need to address: </p> <p><a href="http://us1.php.net/in_array" rel="nofollow noreferrer">http://us1.php.net/in_array</a> <a href="http://www.sitepoint.com/forums/showthread.php?577188-Retrieving-A-Checkbox-Array-from-MySql" rel="nofollow noreferrer">http://www.sitepoint.com/forums/showthread.php?577188-Retrieving-A-Checkbox-Array-from-MySql</a> <a href="https://stackoverflow.com/questions/15756107/populate-checkbox-from-array">Populate checkbox from array</a></p> <p><strong>Thanks in advance!</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload