Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 8 of 8
  1. #1
    Junior Member mhulse's Avatar
    Join Date
    Aug 2004
    Location
    Eugene, Oregon
    Posts
    12
    Member #
    7212
    Title: Current link Dynamically disabled????

    Hello,

    What is the best way to dynamically disable the "current page" link in a CSS based nav?

    I would prefer PHP... no JS...

    Because I am using templates, I do not want to hand style the menu on each page like so:

    Code:
    /*css*/
    	a:active, a.current {
    	color: #666666;
    }
    Code:
    <!-- 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!
    Cheers
    Micky

  2.  

  3. #2
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Don't know if there's any way of doing that without including the php script on each page, which might be just as much work as rewriting each page. (you could write the php piece in a separate file, and use an 'include' on each page.)

    The script would have to process the nav bar on each page, and make the neccessary dynamic changes. If you put the whole navbar in a variable, let php process it and make the neccessary alteration, you could make it rewrite the navbar for each page.
    It would then add the 'current' class to the active link in the menu.

    PHP Code:
    //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



    Might be an easier solution han this, or maybe this is not what you had in mind at all?
    S. Rosland

  4. #3
    Junior Member mhulse's Avatar
    Join Date
    Aug 2004
    Location
    Eugene, Oregon
    Posts
    12
    Member #
    7212
    Quote Originally Posted by rosland
    Don't know if there's any way of doing that without including the php script on each page, which might be just as much work as rewriting each page. (you could write the php piece in a separate file, and use an 'include' on each page.)

    The script would have to process the nav bar on each page, and make the neccessary dynamic changes. If you put the whole navbar in a variable, let php process it and make the neccessary alteration, you could make it rewrite the navbar for each page.
    It would then add the 'current' class to the active link in the menu.
    Hi Rosland,

    Thanks for the respose! I really appreciate it...

    I actually had someone suggest the same technique over at:

    http://www.webmasterworld.com/forum88/4979.htm

    But I was having some troubles getting the class to be dynamically applied to the current links... I am going to impliment your code now (again, thanks)

    Quote Originally Posted by rosland
    Might be an easier solution han this, or maybe this is not what you had in mind at all?
    So far you and one other have suggested simular things, so this must be one of the best ways to do it... And yes, this is exactly what I am looking for, I really appreciate your help! I will post my results soon, I am getting really close.

    Cool, thanks! Cheers
    Micky

  5. #4
    Junior Member mhulse's Avatar
    Join Date
    Aug 2004
    Location
    Eugene, Oregon
    Posts
    12
    Member #
    7212
    Hmmm, I got it working with no errors... but for some reason the class is not being applied to the current page link.

    Here is a link to the test page:

    http://gladstone.uoregon.edu/~mhulse/home_2.php

    I will keep plugging away at this...

    Does anyone know why the class is not being dynamicaly defined?

    Thanks in advance...

    Cheers
    Micky

  6. #5
    Junior Member mhulse's Avatar
    Join Date
    Aug 2004
    Location
    Eugene, Oregon
    Posts
    12
    Member #
    7212
    I ended up getting this code to work first:

    PHP Code:
    <?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");
    ?>
    The class was not being defined because "if($page == $pages[$link]){" were not matching up... but I fixed that by adding "$page = str_replace("/~mhulse/", "", $_SERVER['SCRIPT_NAME']); ".

    Thanks for your help Rosland, I appreciate it... I am still going to work on yours too... It sure is nice not having to update the menu on every page... Now, all I got to do is edit the menu include file, and boom! Updated dynamic CSS navigation!

    Thanks again!
    Cheers!

  7. #6
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    You're welcome!

    I did run a short test of the above mentioned code, and it did alter the link definition to include the class (which again obviously worked as you outlined you wanted it to).

    So my question is; did you look at the source code (view source) on the page beeing generated? If so, did it not include the class at the appropriate link? (it did on all my different link attempts)

    If your testing server is your local machine, make sure you set (if you use Dreamweaver) your local files to be tested as originals, not through a temporary produced file, otherwise your files will look like tmpW00828jdu839 instead of their real name (some_name.php), and the basename(__FILE__) constant will not find a script match.

    If
    the class definition gets written to the HTML output and does not produce the desired result, then you should doublecheck your CSS code. Write a static page containing your active/current link stylesheet, and see what results that produce.

    Like mentioned, the code produced fine results on my test server.

    PS
    Your above posted link (to your test page), did/does not work
    S. Rosland

  8. #7
    Junior Member mhulse's Avatar
    Join Date
    Aug 2004
    Location
    Eugene, Oregon
    Posts
    12
    Member #
    7212
    Quote Originally Posted by rosland
    I did run a short test of the above mentioned code, and it did alter the link definition to include the class (which again obviously worked as you outlined you wanted it to).
    Gosh, you know, I think it is the server I am using... It is a remote University server, and I am not too sure how they have things setup. I do know that I originally had problems with this comparison:

    PHP Code:
    if($page == $pages[$link]){ 
    So, I set up some scalfolding, and tested:

    PHP Code:
    echo $page
    gave me this output:

    Code:
    ~mhulseabout.php
    and

    PHP Code:
    $pages[$link
    gave me this output:

    Code:
    about.php
    Because "~mhulseabout.php" and "about.php" were not matching up, the class was not being applied to the menu items at run time. I have a feeling, the same type of problem was happening with your code (I believe the server I am using was causing the discrepancy.)

    I ended up fixing the comparison problem by adding:

    PHP Code:
    $page str_replace("/~mhulse/"""$_SERVER['SCRIPT_NAME']); 
    Which stripped-out the "/~mhulse/" out of the string "echo $page;" so the script would match the page to a link and apply the class.

    Quote Originally Posted by rosland
    So my question is; did you look at the source code (view source) on the page beeing generated? If so, did it not include the class at the appropriate link? (it did on all my different link attempts)
    I viewed the source of the output and no class was applied... I think your code has a simular comparison problem when I use it on my server...

    My question is this: What code would be optimal for plug and play (your script or script I posted)? Yours is looking really nice, and I would like to use it... In fact, I am planning on getting a new server/host with PHPFreaks, and I bet I will not have any probs with both scripts on a different server...

    Quote Originally Posted by rosland
    If your testing server is your local machine, make sure you set (if you use Dreamweaver) your local files to be tested as originals, not through a temporary produced file, otherwise your files will look like tmpW00828jdu839 instead of their real name (some_name.php), and the basename(__FILE__) constant will not find a script match.
    Ah, thanks for the info... I have been planning on testing my PHP locally, but have yet to get around to setting things up.

    Quote Originally Posted by rosland
    If the class definition gets written to the HTML output and does not produce the desired result, then you should doublecheck your CSS code. Write a static page containing your active/current link stylesheet, and see what results that produce.

    Like mentioned, the code produced fine results on my test server.
    Yeah, I did have a few probs with CSS (ordering), but nothing major, and I did fix it asap:

    Code:
    /*
    Links have to be in this order in your stylesheet:
    a:link
    a:visited
    a:hover
    a:active
    */
    
    a:link {
    	color: #999900;
    	text-decoration: none;
    	padding-bottom: 1px;
    	white-space: nowrap;
    }
    a:visited {
    	text-decoration: none;
    	color: #999900;
    }
    a:hover {
    	color: #CCCC00;
    	background: url(../img/underline.gif) repeat-x 100% 100%;
    }
    a:active {
    	color: #FFFF00;
    }
    
    a.current {
    	color: #666600;
    }
    a.current:hover {
    	color: #CCCC00;
    }
    Quote Originally Posted by rosland
    PS Your above posted link (to your test page), did/does not work
    Ah, sorry about that... If you are still curious to see, here is a link to a test page (WARNING: ugly and slapped together, nothing I plan to use...):

    http://gladstone.uoregon.edu/~mhulse/home.php

    The above menu is using the other code I posted...

    Thanks for all your help, I really have learned a lot, and I am sure that many others will benifit from our thread and your kick-butt PHP guru coding skillz!

    Have a good one!
    Cheers
    Micky

  9. #8
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    You're much too kind.

    Best of luck in sorting it out. Looking forward to see the results of your efforts.

    PS.
    Post again if you have further problems. There are members here who way surpass my mediocre PHP skills.
    S. Rosland


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 04:01 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com