• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Keeping parent menu items active in child pages

#1
Question 
Helo, I have this array of navigation from db, and i've already prepare the array in parent child format. So my question is, how do i keep the parent menu active when i visit the child pages. i'm using Codeigniter. the slug is dynamically defined by the user in admin panel.

here is my dummy array:

PHP Code:
array(3) {
 
 [0]=>
 
 array(4) {
 
   ["id"]=>
 
   int(1)
 
   ["menu_item"]=>
 
   string(5"Item1"
 
   ["slug"]=>
 
   string(6"item-1"
 
   ["parent_id"]=>
 
   int(0)
 
 }
 
 [1]=>
 
 array(5) {
 
   ["id"]=>
 
   int(2)
 
   ["menu_item"]=>
 
   string(5"Item2"
 
   ["slug"]=>
 
   string(6"item-2"
 
   ["parent_id"]=>
 
   int(0)
 
   ["children"]=>
 
   array(1) {
 
     [0]=>
 
     array(5) {
 
       ["id"]=>
 
       int(5)
 
       ["menu_item"]=>
 
       string(5"Item5"
 
       ["slug"]=>
 
       string(6"item-5"
 
       ["parent_id"]=>
 
       int(2)
 
       ["children"]=>
 
       array(1) {
 
         [0]=>
 
         array(4) {
 
           ["id"]=>
 
           int(4)
 
           ["menu_item"]=>
 
           string(5"Item4"
 
           ["slug"]=>
 
           string(6"item-4"
 
           ["parent_id"]=>
 
           int(5)
 
         }
 
       }
 
     }
 
   }
 
 }
 
 [2]=>
 
 array(4) {
 
   ["id"]=>
 
   int(3)
 
   ["menu_item"]=>
 
   string(5"Item3"
 
   ["slug"]=>
 
   string(6"item-3"
 
   ["parent_id"]=>
 
   int(0)
 
 }



here is my routes setup to grab the page:
PHP Code:
$route['([a-zA-Z0-9_-]+)'] = 'pages/render'


for now i only success to get the menu active when visiting the root page by compare the slug with uri_segment(1) with this function:

PHP Code:
function get_menu ($array$child FALSE)
{
 
   $CI =& get_instance();
 
   $str '';

 
   if (count($array)) {
 
       $str .= $child == FALSE '<ul class="nav">' PHP_EOL '<ul class="dropdown-menu">' PHP_EOL;

 
       foreach ($array as $item) {

 
           $active $CI->uri->segment(1) == $item['slug'] ? TRUE FALSE;
 
           if (isset($item['children']) && count($item['children'])) {
 
               $str .= $active '<li class="dropdown active">' '<li class="dropdown">';
 
               $str .= '<a  class="dropdown-toggle" data-toggle="dropdown" href="' site_url(e($item['slug'])) . '">' e($item['title']);
 
               $str .= '<b class="caret"></b></a>' PHP_EOL;
 
               $str .= get_menu($item['children'], TRUE);
 
           }
 
           else {
 
               $str .= $active '<li class="active">' '<li>';
 
               $str .= '<a href="' site_url($item['slug']) . '">' e($item['title']) . '</a>';
 
           }
 
           $str .= '</li>' PHP_EOL;
 
       }

 
       $str .= '</ul>' PHP_EOL;
 
   }

 
   return $str;


Please let me know what is the correct way to achive this.

Thank you very much
Reply


Messages In This Thread
Keeping parent menu items active in child pages - by ahmadmilzam - 11-16-2014, 10:42 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.