mardi 29 janvier 2019

How to set selected properties in multiple checkboxes with foreach loop in PHP?

I want to display all the category and I want to set selected properties to multiple Checkboxes.

<div class="col-md-10">
<?php 
$allCategories = $category->getAllCategory();
if ($allCategories) {
    foreach ($allCategories as $items) {

        $categoryAccess = $category_permitted->getCategoriesByUserId($user_info[0]->id);

        ?>
        <input type="checkbox" name="cat_access[]" value="<?php echo $items->id ?>"<?php echo (isset($categoryAccess) && $categoryAccess->id == $items->id) ? 'selected' : '' ?>><?php echo $items->title?>
        <?php
    }
}
 ?>

I have 5 categories which have checkboxes and for the output, 3 of the categories should be checked. I am getting error in <?php echo (isset($categoryAccess) && $categoryAccess->id == $items->id) ? 'selected' : '' ?> it gives this error: Trying to get property 'id' of non-object and when I do <?php echo (isset($categoryAccess) && $categoryAccess[0]->id == $items->id) ? 'selected' : '' ?> it gives data but of 0 index only.




Aucun commentaire:

Enregistrer un commentaire