Mr J
Mr J

Reputation: 55

Making checkbox checked by exploding value

Man this makes no sense I tried to figure this out for hours and searched all over this site. In the DB, the user_level is 1,2,3 as a value I imploded. I am displaying checkboxes and I want them to be checked when the value matches what is in the DB. This worked perfectly when there was only a single value in the DB like 1 or 2 for user_level. Now with 1,2,3, I simply exploded the value then used in_array() to result a result and if so add the checkbox. For some reason this don't work, random boxes seem to be checked. Here is the code:

$sql = "SELECT user_level FROM table";
$result = query_db($sql);
$row = mysql_fetch_array($result);


$field_names =  array(MEMBER    => 'Member', 
              VISITOR => 'Visitor');


foreach ( $field_names as $key => $value )
{

    $thePostIdArray = explode(',', $key);

    //print_r($thePostIdArray); // testing output


    $selected = ( in_array($row['user_level'], $thePostIdArray) ) ? 'checked="checked"' : '';


    $options .= '<input type="checkbox" name="'.$perm_type.'[]" '.$selected.' value="'.$key.'"> '.$value. ' ';

}

The $selected value should output the "checked" value on the correct checkboxes but it is not.

Upvotes: 1

Views: 4775

Answers (2)

gloomy.penguin
gloomy.penguin

Reputation: 5911

<?php

$db = mysql_connect('localhost', 'root', 'password')
         or die('Could not connect: ' . mysql_error());

$query  = "select user_level from test.my_table";
$result = mysql_query($query,$db); 

/*
mysql> select user_level from my_table;
+------------+
| user_level |
+------------+
| 1,2,3      |
| 4          |
| 5,6        |
+------------+
3 rows in set (0.00 sec)

*/

/*

$rows = array();  
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
{
   $rows[] = $row;
}

print "<pre>".print_r($rows,true)."</pre>"; 


Array
(
    [0] => Array
        (
            [user_level] => 1,2,3
        )

    [1] => Array
        (
            [user_level] => 4
        )

    [2] => Array
        (
            [user_level] => 5,6
        )

)
*/

$my_checkboxes = array('one','two','three','four','five','six','seven'); 
$group = 0; 

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
{ 
   $row_values = explode(',',$row['user_level']); 

   /*
   print "<pre>".print_r($row_values,true)."</pre>"; 

   Array
   (
       [0] => 1
       [1] => 2
       [2] => 3
   )
   Array
   (
       [0] => 4
   )
   Array
   (
       [0] => 5
       [1] => 6
   ) 
   */

   print "<div style='border:1px solid black;display:inline-block'>";

   foreach ($my_checkboxes as $value => $label)
   {
      $selected = (in_array(($value+1), $row_values)) ? "checked='checked'" : ''; 

      print "<input type='checkbox' name='group$group' value='$value' $selected/>$label<br/>";  
   } 

   print "</div><br/><br/>";
   $group++; 
}


?> 

enter image description here

and the HTML the PHP outputs (view source):

<div style='border:1px solid black;display:inline-block'><input type='checkbox' name='group0' value='0' checked='checked'/>one<br/>
<input type='checkbox' name='group0' value='1' checked='checked'/>two<br/>
<input type='checkbox' name='group0' value='2' checked='checked'/>three<br/>
<input type='checkbox' name='group0' value='3' />four<br/>
<input type='checkbox' name='group0' value='4' />five<br/>
<input type='checkbox' name='group0' value='5' />six<br/>
<input type='checkbox' name='group0' value='6' />seven<br/>
</div><br/><br/>

<div style='border:1px solid black;display:inline-block'><input type='checkbox' name='group1' value='0' />one<br/>
<input type='checkbox' name='group1' value='1' />two<br/>
<input type='checkbox' name='group1' value='2' />three<br/>
<input type='checkbox' name='group1' value='3' checked='checked'/>four<br/>
<input type='checkbox' name='group1' value='4' />five<br/>
<input type='checkbox' name='group1' value='5' />six<br/>
<input type='checkbox' name='group1' value='6' />seven<br/>
</div><br/><br/>

<div style='border:1px solid black;display:inline-block'><input type='checkbox' name='group2' value='0' />one<br/>
<input type='checkbox' name='group2' value='1' />two<br/>
<input type='checkbox' name='group2' value='2' />three<br/>
<input type='checkbox' name='group2' value='3' />four<br/>
<input type='checkbox' name='group2' value='4' checked='checked'/>five<br/>
<input type='checkbox' name='group2' value='5' checked='checked'/>six<br/>
<input type='checkbox' name='group2' value='6' />seven<br/>
</div><br/><br/>

Upvotes: 0

Marc B
Marc B

Reputation: 360702

Multiple issues:

array(MEMBER ... is incorrect. MEMBER and VISITOR like that are undefined constants (unless you haven't shown a define() call you're doing elsewhere). PHP will "politely" correct them to be strings, but will issue warnings.

Your foreach() loop then takes this new array and loops on its contents:

foreach ( $field_names as $key => $value ) {
    $thePostIdArray = explode(',', $key);

But at this point $key is just a STRING, e.g. 'MEMBER' or 'VISITOR'. It's not array, so there's nothing to explode on.

$foo = explode(',', 'member');

will produce a single element array containing 0 => 'member', so now you DO have an array. And that, purely by accident, would let your in_array call work. But this entire sequence is pointless and utterly redundant.

Note that in_array does a case-sensitive comparison:

$arr = array('FOO');
var_dump('foo', $arr);

will return a boolean false, because FOO != foo in this case.

Upvotes: 1

Related Questions