1

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 
}
3
  • 1
    "I need to test inside the $categories as $category foreach loop whether an ID needs to be ticked" and how do you know if it needs to be ticked? Commented Apr 21, 2011 at 3:55
  • Hey Dagon. Let's say I tick 2 checkboxes with IDs of 4 & 11, these are updated in myssql perfectly. I then need to the checkboxes 4 & 11 to be ticked all the time, hope that helps Commented Apr 21, 2011 at 4:02
  • check the post global, or where ever they are stored. Commented Apr 21, 2011 at 4:04

1 Answer 1

3

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").

Sign up to request clarification or add additional context in comments.

1 Comment

thanks Jimmy. The data is serialized, is this a problem? $category->cat_ID is called checkbox and looks like this in the database a:5:{s:4:"name";s:13:"Keith Donegan";s:3:"age";s:2:"21";s:7:"college";s:9:"Spin City";s:8:"category";s:1:"7";s:8:"checkbox";a:2:{i:0;s:1:"3";i:1;s:1:"9";}}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.