PHP Multiple checkboxes, find out if checkbox should be checked?

I am outputting some categories in WordPress, these display fine and dandy.
When I tick some checkboxes, these all get updated in the database correctly. I can also output what IDs that we’re ticked in a separate foreach loop; but here is my problem, I need to test inside the $categories as $category foreach loop whether an ID needs to be ticked, but I can’t for the life of me figure out the logic!!

<?php 
$categories = get_categories(); 
foreach($categories as $category)
{ 
    $eirepanel_general_options_string = implode(',', $eirepanel_general_options['checkbox']); // String
    $eirepanel_general_options_array_pieces = explode(',', $eirepanel_general_options_string); // Array of IDs

    echo $category->cat_ID; // String because inside loop

    ?>

    <span><?php echo $category->cat_name; ?></span>
    <input name="eirepanel_general_options_checkbox[]" type="checkbox" value="<?php echo $category->cat_ID; ?>" />
<?php 
}

Related posts

Leave a Reply

1 comment

  1. If $ids contains a list of all currently selected checkboxes, add this snippet to your <input type="checkbox"> line:

    <input type="checkbox"... <?=in_array($category->cat_ID, $ids)? 'checked="checked"' : '' ?> ... >
    

    $ids should look like: array("25", "14", "1").