Welcome Guest, Not a member yet? Register   Sign In
Include a menu on every page? use url helper in a class?
#8

[eluser]NateL[/eluser]
[quote author="John_Betong" date="1256844243"] 
I have just written a common menu for our latest project. It checks the URL against the menu item title and if there is a match then the class changes to highlight the particular page item title.
 

Code:
// CSS file
  #navigation a.on {color:#fff}
  #navigation a.off{color:#bbb}
  #navigation a    {display:inline; background:url(/images/nav.png);text-decoration:none}

  // _top_menu.php file declated in  MY_Controller.php
<?php
  $a =    array
                (
                    'home'      => 'c_five/home',
                    'about'     => 'c_five/about',
                    'archives'  => 'c_five/archives',
                    'contact'   => 'c_five/contact',
                    'writing'   => 'c_five/writing',
                    'subscribe' => 'c_five/subscribe'
                );
    foreach($a as $title => $url):
        $toggle = strpos($this->uri->uri_string, $url) ? 'on' : 'off';
        echo anchor($url, $title, "class='$toggle'") .nbs(15);
    endforeach;                
    unset($toggle);unset($a);
//
 
 
 
edit: spelling and formatting[/quote]

I was JUST trying to figure this part of my site out. Very helpful code for me! Thanks a bunch!

For some reason tho, every instance that gets echoed out in the foreach statement is in the "off" state. Any ideas why?

Code:
$a =    array
      (
          'home'          => '/home',
          'about'         => '/about',
          'portfolio'      => '/portfolio',
          'contact'       => '/contact'
      );
    
    foreach($a as $title => $url):
        $toggle = strpos($this->uri->uri_string, $url) ? ' class="active"' : '';
        echo "<li $toggle>".anchor($url, $title)."</li>";
    endforeach;                
    unset($toggle);
    unset($a);


Messages In This Thread
Include a menu on every page? use url helper in a class? - by El Forum - 10-30-2009, 10:46 PM



Theme © iAndrew 2016 - Forum software by © MyBB