Welcome Guest, Not a member yet? Register   Sign In
Generating a menu..
#4

[eluser]Lone[/eluser]
This is a quick and dirty way to do it - your best way is to get the $items compiled from a DB (using a model) of some sorts using similar field names.

Code:
function build_menu($uri)
    {
        $items = array();
        $items[0]['url']   = '';
        $items[0]['title'] = 'home';
        $items[0]['name']  = 'Home';
        
        $items[1]['url']   = 'blotter';
        $items[1]['title'] = 'blotter';
        $items[1]['name']  = 'TechBlotter';
        
        $items[2]['url']   = 'devblog';
        $items[2]['title'] = 'blog';
        $items[2]['name']  = 'DevBlog';
        
        $menu = '<ul>'."\n";
        
        foreach($items as $item) {
            if(stristr($item['url'], $uri)) {
                $class = ' class="current"';
            } else {
                $class = '';
            }
            $menu .= "\t".'<li><a href="/'.$item['url'].'"'.$class.' title="'.$item['title'].'"><span>'.$item['name'].'</span></a></li>'."\n";
            
        }
        
        $menu .= '</ul>';
        return $menu;
    }

The "/n" and "/t" aren't really required but I like html Smile

EDIT: You just must have edited yours with the solution after I posted! They way you built your array is the best way for just two vars, but I do have a habit at times of preferring a numbered index Tongue


Messages In This Thread
Generating a menu.. - by El Forum - 01-17-2008, 08:59 PM
Generating a menu.. - by El Forum - 01-17-2008, 09:09 PM
Generating a menu.. - by El Forum - 01-17-2008, 10:39 PM
Generating a menu.. - by El Forum - 01-17-2008, 11:26 PM
Generating a menu.. - by El Forum - 01-17-2008, 11:46 PM
Generating a menu.. - by El Forum - 03-04-2009, 01:28 AM



Theme © iAndrew 2016 - Forum software by © MyBB