Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 24 revisions

Category:Help::TipsAndTricks Image:DynamicMenu.gif Menu library. Will show/hide sub-menus depending on in which, uri->segment the user is browsing. Just add it in to autoload.

I call it from the view file like this: [code] $menu = $this->menu->mkmenu(); [/code] Where ever in the view file you like to have your menu you would enter this: [i]Make sure you also load the html helper for ul[/i] [code] <?=ul($menu,array('class' => 'menu','id' => 'menu'));?> [/code] And here you create your menu [code] class Menu { function setVars() { $this->menu = array( anchor('account', 'Account'), anchor('news', 'News'), anchor('ticket', 'Support') );

    $this->sub['account'] = array(
        anchor('account/secquestion','Security'),
        anchor('account/password','Password'),
        anchor('account/user','Profile')
        );
    
    $this->sub['news'] = array(anchor('news/post','Post'));
    
}

function Menu()
{
    log_message('debug', 'Menu: initialized');
}

function mkMenu()
{
    $CI =& get_instance();
    $this->setVars();
    
    $segment = $CI->uri->segment(1);
    foreach($this->menu as $k => $v){
        $menutmp[$k] = $v;
        if (strripos($v, $segment) !== FALSE && isset($this->sub[$segment])){
            $this->menu[$this->menu[$k]] = $this->sub[$segment];
            unset($menutmp[$k]);
            $menutmp[$this->menu[$k]] = $this->sub[$segment];
        }
    }
    return $menutmp;
}

} [/code]

If you make an improvement or have questions post them here: [url=http://www.codeigniter.com/forums/viewthread/3619/]Dynamic Menu Forum[/url]

Clone this wiki locally