gosulove
gosulove

Reputation: 1645

PHP set the checkbox to be checked if data in Array 1 equal Array 2

I have array that's retrieved from mysql.

Array item_category is an Array that shows all the categories.

Array selected_category is an array that shows the selected categories.

$item_category[32]="western food";  
$item_category[33]="chinese food";  
$item_category[34]="mix food";     
$item_category[35]="japanese food"; 
$item_category[36]="korean food";   
$item_category[37]="italian food";  

$selected_category[32]="western food"; 
$selected_category[33]="chinese food";  
$selected_category[34]="mix food";    

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

    echo '<input type="checkbox" name="check_list[] "value="'.$key.'"> '.$value.'<br>';

}

enter image description here

  1. use foreach loop to display out all the categories

  2. check the checkbox in Array $selected_category.

So my question is how to check the checkbox just like the screenshot since they have the same "Key" in these 2 Arrays? I try something with the following but it doesn't work as what I expected.

$checked = true ? "checked" : "";
echo '<input type="checkbox" ' . $checked . ' name="check_list[] "value="'.$key.'"> '.$value.'<br>';

Upvotes: 0

Views: 1402

Answers (3)

Nikhil Vaghela
Nikhil Vaghela

Reputation: 2096

Try below code it's works for you.

<?php
$item_category[32]="western food";  
$item_category[33]="chinese food";  
$item_category[34]="mix food";     
$item_category[35]="japanese food"; 
$item_category[36]="korean food";   
$item_category[37]="italian food";  

$selected_category[32]="western food"; 
$selected_category[33]="chinese food";  
$selected_category[34]="mix food";    

foreach ($item_category as $key => $value) {
      echo '<input type="checkbox" '.(isset($selected_category[$key])? 'checked' : '').' name="check_list[] "value="'.$key.'"> '.$value.'<br>';

}

Upvotes: 1

Blazeag
Blazeag

Reputation: 480

Try this:

$item_category[32]="western food";  
$item_category[33]="chinese food";  
$item_category[34]="mix food";     
$item_category[35]="japanese food"; 
$item_category[36]="korean food";   
$item_category[37]="italian food";  

$selected_category[32]="western food"; 
$selected_category[33]="chinese food";  
$selected_category[34]="mix food";    

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

    echo '<input type="checkbox" name="check_list[] "value="' . $key . '" ' . ((in_array($value), $selected_category) ? 'checked="checked"' : '') . '>    '.$value.'<br>';

}

in_array() function returns true if one specified value is present in one specified array

Upvotes: 1

u_mulder
u_mulder

Reputation: 54796

You need to check if $key of $item_category presents in $selected_category. This can be done with isset function:

foreach ($item_category as $key => $value) {
    // check if `$key` set in `$selected_category` 
    $checked = isset($selected_category[$key])? 'checked' : '';

    echo '<input type="checkbox" ' . $checked .' name="check_list[] "value="'.$key.'"> '.$value.'<br>';
}

Upvotes: 1

Related Questions