Reputation: 749
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
Upvotes: 0
Views: 51
Reputation: 26421
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; ?>
Upvotes: 2
Reputation: 1094
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>
Upvotes: 0