0

So, I have a variable which have all the menu_ids. So what I want to show is - Check with the current variable i.e $get_all_menu_values and if the value is there, then mark it tick(checked). Else leave it blank.

   <?php    
    $get_all_menu_values = "1,2,3,4,5"; ?>
    <ul>
    <?php foreach ($getSubMenuValues as $sub_menu): ?>
    <li class='has-sub'><a href='<?=$sub_menu['menu_url']; ?>'><span><?=$sub_menu['menu_name']; ?></span></a></li>
    <div align="center"><input type="checkbox" class="form" value="" name="get_menu_values[]" /></div>
    <?php endforeach; ?>   
    </ul>

In other words - check with the $get_all_menu_values and if the m_id is the same make it checked. More or less checking with IN array I guess. But don't know how to do that. Any help.

Thanks, Kimz

1
  • Is $get_all_menu_values an array or string? Commented May 5, 2014 at 11:04

2 Answers 2

2

Yes, in_array is what you need,

<?php
 $get_all_menu_values_array = explode(",",$get_all_menu_values); 
 foreach ($getSubMenuValues as $sub_menu):
   $isChecked = '';
   if(in_array($sub_menu['m_id'],$get_all_menu_values_array) {
      $isChecked = "checked='checked'";
   } 
?>
<li class='has-sub'><a href='<?=$sub_menu['menu_url']; ?>'><span><?=$sub_menu['menu_name']; ?></span></a></li>
<div align="center"><input type="checkbox" class="form" value="" name="get_menu_values[]" <?=$isChecked; ?> /></div>
<?php endforeach; ?>   
Sign up to request clarification or add additional context in comments.

Comments

0

Use in_array to check values

    $get_all_menu_values = "1,2,3,4,5";
    <ul>
    <?php foreach ($getSubMenuValues as $sub_menu){ ?>
    <li class='has-sub'><a href='<?=$sub_menu['menu_url']; ?>'><span><?=$sub_menu['menu_name']; ?></span></a></li>
    <div align="center"><input type="checkbox" class="form" value="" <?php (in_array($sub_menu["m_id"], $get_all_menu_values) ? "checked='checked'" : ""; ?> name="get_menu_values[]" /></div>
    <?php } ?>   
    </ul>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.