开发者

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

开发者 https://www.devze.com 2023-02-28 06:49 出处:网络
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

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 l开发者_StackOverflow中文版ogic!!

<?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 
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号