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

<?php 

$allCategories = $category->getAllCategory();

if ($allCategories) {

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

foreach ($allCategories as $key => $items) {
    if ($categoryAccess) {
        foreach ($categoryAccess as $key => $value) {
            ?>
            <input type="checkbox" name="cat_access[]" value=" <?php echo $items->id ?> " <?php echo (isset($categoryAccess) && @$categoryAccess[$key]->id == $items->id) ? 'checked' : '' ?>><?php echo $items->title?>
            <?php
        }
    }

?>

<?php
}
}
 ?>

After looping through $categoryAccess I got two options selected but I am getting all category repeating twice as an output i.e category1 category 1 category 2 category 2 category 3 category 3 category 4 category 4.

(Added [code] tags ~ Mod)

    I'd guess that you want the bit that outputs the category to be after (or before?) the inner foreach() loop, in which case within that inner loop you may want to echo whatever the "access" is for that checkbox?

      I'm not even sure the nested loop is correct: the inner loop repeats every time the outer loop is iterated, and it's the repetition that's the problem. I think what is wanted is one loop over all categories that contains a check on each one to see if it is in the list of "accessed" categories.

        Write a Reply...