tags:

views:

184

answers:

6

I need to insert a class named "current" into a list like below depending on what page I am on.
All pages on my site are included into the index page like this,
Index.php includes a header file then it includes the body file and then footer file.
The body file will be one of the pages below.
It is included through the page like this in the url index.php?p=home
So I can easily get the page variable $p and know when I am on a certain page

So what would be the BEST way to add the "current" css class to a navigation list like below?

<li class="drop"><a href=""><em>Home</em></a></li>
<li class="drop"><a href=""><em>inbox</em></a></li>
<li class="drop"><a href=""><em>outbox</em></a></li>
<li class="drop"><a href=""><em>online users</em></a></li>
<li class="drop"><a href=""><em>all users</em></a></li>
<li class="drop"><a href=""><em>forums</em></a></li>
<li class="drop"><a href=""><em>blogs</em></a></li>
<li class="drop"><a href=""><em>bulletins</em></a></li>
<li class="drop"><a href=""><em>news</em></a></li>

...this is just a mockup list the real list is more complex


UPDATE: I don't know if an array is the way to go or not, Above is a quick example, below is my ACTUAL menu list, there are submenus embeded into the upper level list items, so if I am on any page in a submenu, the main menu should have the "current" class added to it, so doing an array is somewhat complex?

    <div id="bottomrow"> 
      <div class="pad"> 
        <ul class="menu left">
          <li class="first"><a href="/"><em>Home</em></a></li>
          <li class="current users drop"><a href=""><em>Users</em></a><span class="drop">&nbsp;</span> 
        <ul id="moreheader">
              <li><a href=""><em>Widgets</em></a></li>
              <li><a href=""><em>News</em></a></li>
              <li><a href=""><em>Promote</em></a></li>
              <li><a href=""><em>Development</em></a></li>
              <li><a href=""><em>Bookmarks</em></a></li>
              <li><a href=""><em>About</em></a></li>
            </ul>
       </li>
          <li><a href=""><em>Forums</em></a></li>
          <li class="drop current"><a href="/moreheader"><em>More</em></a><span class="drop">&nbsp;</span> 
            <ul id="moreheader">
              <li><a href=""><em>Widgets</em></a></li>
              <li><a href=""><em>News</em></a></li>
              <li><a href=""><em>Promote</em></a></li>
              <li><a href=""><em>Development</em></a></li>
              <li><a href=""><em>Bookmarks</em></a></li>
              <li><a href=""><em>About</em></a></li>
            </ul>
          </li>
          <li class="moneytabmenu"><a href="/moneytabmenu"><em>Money:<span class="moneytabmenu-total">$0.00</span></em></a></li>
        </ul>
        <ul class="menu right">
          <li class="drop myaccount"><a href="" class="first"><img class="avatar" src="http://gravatar.com/avatar.php?gravatar_id=7ab1baf18a91ab4055923c5fd01d68a2&amp;amp;rating=pg&amp;amp;size=80&amp;amp;default=" height="19" width="19" alt="you" /><em>My 
            Account</em></a><span class="drop">&nbsp;</span> 
            <ul id="myaccount">
              <li><a href=""><em>Dashboard</em></a></li>
              <li><a href=""><em>Account Settings</em></a></li>
              <li><a href=""><em>Settings</em></a></li>
            </ul>
          </li>
          <li class="drop"><a href=""><em>Mail</em></a><span class="drop">&nbsp;</span> 
            <ul id="mailboxheader">
              <li><a href=""><em>InBox</em></a></li>
              <li><a href=""><em>SentBox</em></a></li>
              <li><a href=""><em>Trash</em></a></li>
              <li><a href=""><em>Post Bulletin</em></a></li>
              <li><a href=""><em>View Bulletins</em></a></li>
            </ul>
          </li>
          <li class="drop"><a href=""><em>More</em></a><span class="drop">&nbsp;</span> 
            <ul id="moreheader">
              <li><a href=""><em>Widgets</em></a></li>
              <li><a href=""><em>News</em></a></li>
              <li><a href=""><em>Promote</em></a></li>
              <li><a href=""><em>Development</em></a></li>
              <li><a href=""><em>Bookmarks</em></a></li>
              <li><a href=""><em>About</em></a></li>
            </ul>
          </li>
        </ul>
      </div>
    </div>
    <!-- END div#bottomrow -->
  </div>
A: 

I suggest that instead of maintaining a list file of <li> items like that, you should put the items into an array and generate them dynamically.

$arr = array(
    'Home' => 'url/to/home',
    'inbox' => 'url/to/inbox',
    //etc
);

foreach($arr as $name => $url)
{
    echo '<li class="drop';
    if ($name == $p) echo ' curPage';
    echo '"><a href="'.$url.'"><em>'.$name.'</em></a></li>';
}

You can use whatever comparison criteria that you need there while generating the loop in order to compare to $p and determine that it's the current page.

zombat
I don't think I could do it this way because my list menu is actually a lot different then what I have posted above, in addition to the <li> items, some of them have embeded <li> items to create dropdown list, do you know any other methods?
jasondavis
I have updated my post with the code from my menu. The only thing I can think of is to possibly make an array of each menu item that would list all submenu items for it, then if it is on 1 of theose page use something like this <?=$current_more?> into the menu toplevel part
jasondavis
A: 

A rough example of how you might go about it

$menu = array(
    'Home'         => '#'
  , 'inbox'        => '#'
  , 'outbox'       => '#'
  , 'online users' => '#'
  , 'all users'    => '#'
  , 'forums'       => '#'
  , 'blogs'        => '#'
  , 'bulletins'    => '#'
  , 'news'         => '#'
);

$current = "outbox";

foreach ( $menu as $label => $url )
{
  $css = ( $label == $current ) ? 'drop current' : 'drop';
  echo '<li class="', $css, '"><a href="', $url, '"><em>', $label, '</em></a></li>';
}
Peter Bailey
I have updated my post, I don't think an array would be possible in my situation?
jasondavis
+1  A: 

first of all i would place them in an array:

$pages = array();
$pages['home'] = array(
    'name' => 'Home',
    'url' => '......',
    'submenus' => array(
       'test-submenu' = array(
          'name' => 'Test Submenu',
          'url' => '.....',
       )
    )
);
$pages['inbox'] = array(
    'name' => 'Inbox',
    'url' => '......',
);

then in my html i would do :

<?php foreach ($pages as $section => $page) { ?>
    <li class="drop <?php echo ($_GET['section'] == $section) ? 'current' : ''; ?>">
        <a href="<?php echo $page['url'] ?>"><em><?php echo $page['name'] ?></em></a>
    </li>  
<?php } ?>
solomongaby
I have updated my post, I don't think an array would be possible in my situation?
jasondavis
why not ... you can make a multiple dimension array ...
solomongaby
A: 

I did something similar recently using JavaScript where I wanted to change the id of the element that matched the page name to 'cur' so it would attract the appropriate styling. I used php's $_SERVER["PHP_SELF"] to get the page name, JavaScript to strip it of the first slash and then split it using '.' as a delimeter, giving me the current page name. Then, because I was resetting the id, I had to duplicate the current node and swap the old for the new (you can't dynamically change id). For class, it's way easier, but it depends how you intend to know which ones to set the className for. Is JavaScript an option? If it is, you're laughing. Doing it purely in PHP, there are too many factors to consider without knowing how you are populating your lists.

+3  A: 

Not a really good solution, but if you had an id corresponding to your $p variable on each of your menu items, like this:

<li id="home"><a href=""><em>Home</em></a></li>
<li id="inbox"><a href=""><em>inbox</em></a></li>

then you could either output an appropriate <style> tag with your PHP script (shown here for $p = 'home'):

<style type="text/css">#home { /* Your "current" styling here */ }</style>

or set an id on the body tag:

<body id="page_home">

and put this in your main style sheet:

#page_home #home,
#page_inbox #inbox,
...
{
    /* Your "current" styling here */
}
Ölbaum
+1  A: 

if you use same name in script and menu option try this, simple recursive:

    $menu = array(
'home',
'users',
array('widgets','news','promote','development','bookmarks'),
'wrapper',
'forums',
array('widgets','news','promote','development','bookmarks'),
'contact'
);

function li_menu($arrItens){
    preg_match("/[^\/]\/(.*?)\./",$_SERVER['PHP_SELF'],$current);
    echo "<ul>";
    foreach($arrItens as $title){
     if ( is_array($title) ) {li_menu($title);} else {
      $cur = ( $title == $current[1] ) ? 'current' : 'drop';
      echo '<li class="'. $cur . '"><a href="'. $title. '.php"><em>'. $title .'</em></a></li>';
     }
    }
    echo "</ul>";
}
li_menu($menu);
OctaneFX