mysql - looping array value with function php -
i'm trying create multi level dropdown menu mysql store data, , use twig theme engine, know there ton of code outside but, of them html output, since use twig, need array output, , let twig render it,(or maybe there other option, let me know if so).
code work if use html output. if change array output problem 2nd menu level show 1 array or first array, not loop.
array output mysql query ,
array ( [0] => array ( [id] => 1 [title] => dashboard [link] => 1.html [parent_id] => 0 ) [1] => array ( [id] => 2 [title] => master data [link] => 2.html [parent_id] => 0 ) [2] => array ( [id] => 3 [title] => submaster [link] => 11.html [parent_id] => 2 ) [3] => array ( [id] => 4 [title] => submaster [link] => 12.html [parent_id] => 2 )
and here code trying convert, mean before code html output (ul>li>ul>li>/li>/ul>/li>/lu)
$id = ''; function sub($items, $id){ foreach($items $item){ if($item['parent_id'] == $id){ return array("link" =>$item['link'],"title"=>$item['title']); sub($items, $item['id']); } } } foreach($allmenu $data){ if($data['parent_id'] == 0){ $id = $data['id']; $utama[]= array("link"=>$data['link'],"title"=>$data['title'],"sub"=>sub($allmenu, $id)); } }
and output above code
array ( [links] => array ( [0] => array ( [link] => 1.html [title] => dashboard [sub] => ) [1] => array ( [link] => 2.html [title] => master data [sub] => array ( [link] => 11.html [title] => kampus ) ) ) )
see.. in 2nd array (master data) should 2 array right? link 11.html , 12.html.
if there mistake code let me know, if there link job, let me know. thanks
hope work
function sub($items, $id){ foreach($items $item){ if($item['parent_id'] == $id){ $sub = sub($items, $item['id']); if(!empty($sub)) return array("link" =>$item['link'],"title"=>$item['title'],"sub"=>$sub); return array("link" =>$item['link'],"title"=>$item['title']); } } } foreach($allmenu $data){ if($data['parent_id'] == 0){ $id = $data['id']; $utama[]= array("link"=>$data['link'],"title"=>$data['title'],"sub"=>sub($allmenu, $id)); } }
Comments
Post a Comment