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] I use a header.php and footer.php so I have to do this onley once in my heade.php in any case 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' => 'Navbar','id' => 'Navbar'));?> [/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] Here is the style code [code] #HeaderNav { border: 1px solid #000000; background: #333333; color: #ffffff; }

#HeaderNav a { border: 1px solid #000000; background: #333333; color: #ffffff; }

#HeaderNav a:hover { border: 1px solid #000000; background: #333333; color: #ffffff; }

#Navbar {

}

#Navbar ul.menu { /* font-family: Verdana, Arial, Helvetica, sans-serif; / font-size: .8em; font-style: normal; line-height: 1.3em; font-weight: normal; font-variant: normal; text-transform: none; color: #00CC33; text-decoration: none; background-color: #CCCCCC; text-indent: 0em; / list-style-position: inside; */ list-style-position: outside; list-style-type: none; padding: 0 0 0 0; margin: 0 0 0 0; }

#Navbar li { margin: 0 0 0 0; list-style-type: none; } #Navbar a { display: block; padding: 2px 2px 2px 1em; border: 1px solid #000000; background: #dcdcdc; text-decoration: none; /remove the link underlines/ }

#Navbar a:link, #list-menu a:active, #list-menu a:visited { color: #000000; }

#Navbar a:hover { border: 1px solid #000000; background: #333333; color: #ffffff; } [/code]

If you make an improvement or have questions post them here:

[url=http://codeigniter.com/forums/viewthread/48152/]Dynamic Menu Forum[/url]

Clone this wiki locally