Page 1 of 1

Intelligent menu code not recognizing "active" CSS

Posted: Mon Mar 27, 2006 12:19 pm
by bruceg
I am using the following code for my menu navigation.

Code: Select all

<?php
$menu = <<<MENU
<ul id="navlist">
<li class="first"><a href="About_Me.php" title="you know you want to learn more about me">About Me</a></li>
<li><a href="Skillset.php" title="I've got skillz">Skill set</a></li>
<li><a href="Hireme.php" title="I can do wonders for your web presence">Hire Me</a></li>
<li><a href="Portfolio.php" title="web sites, graphics, newsletters">Portfolio</a></li>
<li><a href="Contact.php"  title="how to get in touch with me">Contact</a></li>
<li><a href="Resume.php"  title="my beautiful resume">R&eacute;sum&eacute;</a></li>
<li><a href="http://inspiredevolution.blogs.com/inspiredevolutioncom/"  title="the blog of Inspired-Evolution.com" >Blog</a></li>
<li class="last"><a href="RSS.php"  title="Syndication that is really simple" >RSS</a></li>

</ul>
MENU;
$lines = split("\n", $menu);
foreach ($lines as $line) {
$current = false;
preg_match('/href="([^"]+)"/', $line, $url);
if (substr($_SERVER["REQUEST_URI"], 0, 5) == substr($url[1], 0, 5)) {
$line = str_replace('<a h', '<a id="current" h', $line);
}
echo $line."\n";
}
?>
The code is from photoMatt. http://www.photomatt.net/scripts/intellimenu/

My version can be seen here:
http://www.inspired-evolution.com/About_Me.php

the CSS is supposed to be different to indicate the page you are on using the following:

ul#navlist li#active a#current
{
color: #fff;
background-color: #789;
padding:.5em 1em .5em 1em;

}

I also tried just using a#current and that doesn't work either.

I was hoping maybe someone else is using this type of menu code and can offer suggestions as to what I am doing wrong.

Posted: Mon Mar 27, 2006 12:49 pm
by matthijs
You have to add the slashes before the URLs. Also, I give a variant which replaces the href so that the current link cannot be clicked. And which gives the var pagetitle the value of the current page. can be useful

Code: Select all

<?php
$menu = <<<MENU
<ul id="navlist">
<li class="first"><a href="/About_Me.php" title="you know you want to learn more about me">About Me</a></li>
<li><a href="/Skillset.php" title="I've got skillz">Skill set</a></li>
<li><a href="/Hireme.php" title="I can do wonders for your web presence">Hire Me</a></li>
<li><a href="/Portfolio.php" title="web sites, graphics, newsletters">Portfolio</a></li>
<li><a href="/Contact.php"  title="how to get in touch with me">Contact</a></li>
<li><a href="/Resume.php"  title="my beautiful resume">R&eacute;sum&eacute;</a></li>
<li><a href="http://inspiredevolution.blogs.com/inspiredevolutioncom/"  title="the blog of Inspired-Evolution.com" >Blog</a></li>
<li class="last"><a href="RSS.php"  title="Syndication that is really simple" >RSS</a></li>

</ul>
MENU;
$lines = split("\n", $menu);

foreach ($lines as $line) {
    $current = false;
    preg_match('/href="([^"]+)"/', $line, $url);
    if (substr($_SERVER["REQUEST_URI"], 0, 5) == substr($url[1], 0, 5)) {
        $line = str_replace('a h', 'a id="current" h', $line);
    }
    echo $line."\n";
}


// below the variant:

foreach ($lines as $line) {
        $current = false;
        preg_match('/href="([^"]+)"/', $line, $url);
        if (substr($_SERVER["REQUEST_URI"], 0, 5) == substr($url[1], 0, 5)) {
                $line = str_replace('a h', 'a id="current" h', $line);
                if ($_SERVER["REQUEST_URI"] == $url[1])
                        $line = preg_replace('|href=".*?"|', '', $line);
                $pagetitle = preg_replace('/<[^>]+>/', '', $line);
                echo $line;
        } else {
                echo $line . "\n";
        }
}
echo '<br>' . $pagetitle;
?>