0

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>';

3 Answers 3

1

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>';
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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>';

}

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.