Forum Moderators: coopster
Hello,
What is the best way to (underline) dynamically (/underline) disable the "current page" link in a CSS based nav?
I would prefer PHP... no JS...
Because I am using templates, I (underline) do not (/underline) want to hand style the menu on each page like so:
/*css*/
a:active, a.current {
color: #666666;
}
<!-- HTML --><div id="nav_banner">
<a href="#" class="current">Home</a>
<a href="#">About</a>
<a href="#">Folio</a>
<a href="#">Resume</a>
<a href="#">Code</a>
<a href="#">Forum</a>
<a href="#">Contact</a>
<a href="#">Links</a>
</div>
Any help/links/tuts/code would be greatly appreciated!
Thanks! :D
Cheers
Micky
function crLinks($link){
$pages = array ("About" => "about.php", "Folio" => "folio.php", "Resume" => "resume.php", "Code" => "code.php"); // add the links here
$page = str_replace("/", "", $_SERVER['SCRIPT_NAME']);
if($page == $pages[$link]){
echo $link;
} else {
echo "<a href=\"$pages[$link]\">$link</a>";
}
}
You can call the function for each link this way
crLinks("About"); crLinks("Folio"); ... so on.
I'm sure there's a better way to do this. ;)
Each of the three pages uses a PHP iclude to display menu.php, like so:
<?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">";?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="css/links.css" rel="stylesheet" type="text/css" />
</head><body>
<?php include("./menu.php");?>
</body>
</html>
And here is the menu.php code:
(menu.php)
<?php
function crLinks($link){
$pages = array ("About" => "about.php", "Folio" => "folio.php", "Resume" => "resume.php"); // add the links here
$page = str_replace("/", "", $_SERVER['SCRIPT_NAME']);
if($page == $pages[$link]){
echo "<a href=\"$pages[$link]\" class=\"current\">$link</a> ";
} else {
echo "<a href=\"$pages[$link]\">$link</a> ";
}
}
crLinks("About"); crLinks("Folio"); crLinks("Resume");
?>
And here is my CSS:
/* CSS Document */
a:link {
color: #999900;
text-decoration: none;
}
a:hover {
color: #CCCC00;
text-decoration: underline;
}
a:active {
color: #CCCC00;
}
a:visited {
color: #999900;
text-decoration: none;
}
a.current {
color: #000000;
text-decoration: underline;
} Hmmm, seems to not add the class to the current link... maybe I need to play around with my chmod settings...
Anyway, thanks for your help! I really really appreciate it! :)
If I can get this working I will post the final code for all to use...
Thanks!
Cheers
Micky
[edited by: coopster at 5:53 pm (utc) on Sep. 7, 2004]
[edit reason] No urls please (TOS [webmasterworld.com]) [/edit]
[Posted by rosland]
//The below could be placed in a separate file, and retrieved
//through an include./* The hashmarks (#) would obviously be replaced by real links.
The code would then see what page you're actually on, and add
'class="current"' to the appropriate nav link.
*/
$base_nav="<div id=\"nav_banner\">
<a href=\"#\">Home</a>
<a href=\"#\">About</a>
<a href=\"#\">Folio</a>
<a href=\"#\">Resume</a>
<a href=\"#\">Code</a>
<a href=\"#\">Forum</a>
<a href=\"#\">Contact</a>
<a href=\"#\">Links</a>
</div>";
$link=basename(__FILE__);
$nav_banner=preg_replace("/$link/", "$link\" class=\"current", $base_nav);
echo $nav_banner;
Your (zipper) and the above code work well, with no errors, but for some reason, the CSS class "current" is not being added dynamicaly to the current page link.
Any help would be appreciated. :D
I will post finalized code one I/we get this figured out.
Thanks in advance!
Cheers
Micky
Anyway, here are the two results:
echo $page; gives me this output:
~mhulseabout.php; the true path should be: ~mhulse/about.php
and
$pages[$link] gives me this output:
about.php Obviously the two do not match up... thanks for pointing this out to me...
I will try to get that fixed, hopefully tha is all that needs to be corrected.
Thanks!
Cheers
Micky
# Make your change here:
$page = str_replace("~mhulse/", "", $_SERVER['SCRIPT_NAME']);
if($page == $pages[$link]){
echo "<a href=\"$pages[$link]\" class=\"current\">$link</a> ";
} else {
echo "<a href=\"$pages[$link]\">$link</a> ";
}
}
crLinks("About"); crLinks("Folio"); crLinks("Resume");
?>
Here is the final code, works great, thanks for the help guys! :)
<?php
function crLinks($link){
$pages = array ("About" => "about.php", "Folio" => "folio.php", "Resume" => "resume.php"); // add the links here
$page = str_replace("/~mhulse/", "", $_SERVER['SCRIPT_NAME']);
//$page = str_replace("/", "", $_SERVER['SCRIPT_NAME']);
if($page == $pages[$link]){
echo "<a href=\"$pages[$link]\" class=\"current\">$link</a> ";
} else {
/*echo $link;*/
echo "<a href=\"$pages[$link]\">$link</a> ";
}
}
crLinks("About"); crLinks("Folio"); crLinks("Resume");
?>
Thanks again all, you guys rock!