Forum Moderators: coopster

Message Too Old, No Replies

Current link Dynamically disabled?

         

mhulse

3:59 am on Sep 7, 2004 (gmt 0)

10+ Year Member



Title: Current link Dynamically disabled?

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>
&nbsp;
<a href="#">About</a>
&nbsp;
<a href="#">Folio</a>
&nbsp;
<a href="#">Resume</a>
&nbsp;
<a href="#">Code</a>
&nbsp;
<a href="#">Forum</a>
&nbsp;
<a href="#">Contact</a>
&nbsp;
<a href="#">Links</a>
</div>


I would like to have a dynamic nav that knows to disable/un-link the current link....

Any help/links/tuts/code would be greatly appreciated!

Thanks! :D
Cheers
Micky

Zipper

6:52 am on Sep 7, 2004 (gmt 0)

10+ Year Member



Not sure how ur template works, but how abt something like this..

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. ;)

Zipper

12:14 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



just realized your 'disable link' actually meant applying a style.
replace

echo $link;

with

echo "<a href=\"$pages[$link]\" class=\"current\">$link</a>";

mhulse

4:34 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



Oooh, looks good! Thanks for the code, I really appreciate your help... testing now.

:)

Cheers
Micky

mhulse

5:38 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



Ooooooh, so close but no cigar. :(

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>&nbsp;";
} else {
echo "<a href=\"$pages[$link]\">$link</a>&nbsp;";
}
}
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]

mhulse

7:08 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



Suggestion from a person at another forum:

[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>
&nbsp;
<a href=\"#\">About</a>
&nbsp;
<a href=\"#\">Folio</a>
&nbsp;
<a href=\"#\">Resume</a>
&nbsp;
<a href=\"#\">Code</a>
&nbsp;
<a href=\"#\">Forum</a>
&nbsp;
<a href=\"#\">Contact</a>
&nbsp;
<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

jatar_k

7:11 pm on Sep 7, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it is your comparison that isn't working then

$page == $pages[$link]

echo $page and also echo $pages[$link] to see if they are exactly the same.

mhulse

7:38 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



Ah, they are not the same (dang, I should have set up some scalfolding to have caught this earlier! Sorry to be such a PHP newb.)

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

StupidScript

9:07 pm on Sep 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php
function crLinks($link){
$pages = array ("About" => "about.php", "Folio" => "folio.php", "Resume" => "resume.php"); // add the links here

# Make your change here:
$page = str_replace("~mhulse/", "", $_SERVER['SCRIPT_NAME']);

if($page == $pages[$link]){
echo "<a href=\"$pages[$link]\" class=\"current\">$link</a>&nbsp;";
} else {
echo "<a href=\"$pages[$link]\">$link</a>&nbsp;";
}
}
crLinks("About"); crLinks("Folio"); crLinks("Resume");
?>

Zipper

10:30 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



Sorry, I was away for a while. The initial code was just a guide assuming that ur linking only using the page name. However the change StupidScript pointed out should do the trick. Let us know ur progress.

mhulse

10:43 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



Ah, beautiful...

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>&nbsp;";
} else {
/*echo $link;*/
echo "<a href=\"$pages[$link]\">$link</a>&nbsp;";
}
}
crLinks("About"); crLinks("Folio"); crLinks("Resume");
?>

Thanks again all, you guys rock!