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 only once in my header.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() { $uid = $this->db_session->userdata('uid');

$this->menu = array(
       '1' => anchor('news', 'News'),
       '4' => anchor('ticket', 'Support'),
       '5' => anchor('test', 'Test'),
);

    if($this->uid > 0){
        $this->['3'] = anchor('contact/person', 'Profile');
        $this->sub['contact'] = array(
            anchor('contact/person','User'),
            anchor('contact/company','Comapny'),
            anchor('contact/secquestion','Security'),
            anchor('contact/password','Password')
            );
}       
    
    $this->sub['news'] = array(anchor('news/post','Post'));
    
}

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

function mkMenu()
{
    $CI =& get_instance();
    $this->uid = $CI->db_session->userdata('uid');
    $this->setVars();
    ksort($this->menu);
    $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