Dynamic menu from PHP array -


hey have question dynamic menu created in php. code stackoverflow, want parent styled red color if children of parent selected, here code:

$menu = array(     array(         'title' => 'home',         'link' => 'a'     ),     array(         'title' => 'parent',         'link' => 'b',         'children' => array(             array(                 'title' => 'sub 1',                 'link' => 'c'             ),             array(                 'title' => 'sub 2',                 'link' => 'd'             ),         )     ) );  function buildmenu($menuarray) {     foreach ($menuarray $node)     {         $selected = ($node['link']== $_get['menu']) ? $selected = 'style="color: red;"' : null;         echo "<li ".$selected."><a href='?menu=".$node['link']."'/>" . $node['title'] . "</a>";         if ( ! empty($node['children'])) {             echo "<ul>";             buildmenu($node['children']);             echo "</ul>";         }         echo "</li>";     } } buildmenu($menu); 

so how needs go:

home
parent - selected
sub 1 - selected
sub 2

or

home
parent - selected
sub 1
sub 2 - selected

hope understand want? if children under parent selected parent needs selected.

i have added 1 function check element in children array. may better solution there. @ quick solution :)

$menu = array(     array(         'title' => 'home',         'link' => 'a'     ),     array(         'title' => 'parent',         'link' => 'b',         'children' => array(             array(                 'title' => 'sub 1',                 'link' => 'c'             ),             array(                 'title' => 'sub 2',                 'link' => 'd'             ),         )     ) );  function buildmenu($menuarray) {     foreach ($menuarray $node) {          $getmenu = isset($_get['menu']) ? $_get['menu'] : '';         $checkparent = (isset($node['children']) && !empty($node['children'])) ? checkinchildarray($getmenu, $node['children']) : '';         $parentselected = ($checkparent) ? $selected = 'style="color: red;"' : null;         echo "<li " . $parentselected . "><a href='?menu=" . $node['link'] . "'>" . $node['title'] . "</a></li>";         if (isset($node['children']) && !empty($node['children'])) {              echo "<ul>";             foreach ($node['children'] $submenu) {                 $childselected = ($submenu['link'] == $getmenu) ? $selected = 'style="color: red;"' : null;                 echo "<li " . $childselected . "><a href='?menu=" . $submenu['link'] . "'>" . $submenu['title'] . "</a></li>";             }             echo "</ul>";         }         echo "</li>";     } }  // checking if selected menu inside children array.   function checkinchildarray($needle, $haystack, $strict = false) {     foreach ($haystack $item) {         if (($strict ? $item['link'] === $needle : $item == $needle) || (is_array($item) && checkinchildarray($needle, $item, $strict))) {             return true;         }     }      return false; }   echo buildmenu($menu); 

working demo


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -