Can anyone explain, how this cat array is getting the id and name?
foreach ($all_categories as $menu) { ?>
<input type="checkbox" id="cat_<?php echo $menu->id; ?>" value="<?php echo $menu->id.'_'.$menu->name; ?>" name="cat[]">
..
..
}
and i can get the id and name in controller,by using like below. It is working totally fine. My question is, how id
went to cat array
in position 0,and name position 1?
$res_arr=explode('_',$value);
$cat_id=$res_arr[0];
$cat_name=$res_arr[1];
The explode
function returns an array
either it finds the delimiter
or not. You gave a string
to explode
and asked it to split
the string
wherever it sees '_'
. So in your case there was only one '_'
and explode
sliced the string
into two pieces and placed them in an array
respectively at [0]
and [1]
.
Edit:
<?php
foreach ($all_categories as $menu) {
print "<input type=\"checkbox\" id=\"cat_".$menu->id."\" value=\"".$menu->id."_".$menu->name."\" name=\"cat[]\"/>";
}
?>
php short tag is not recommended Link