www.webdeveloper.com
Results 1 to 5 of 5

Thread: Active State Same Nav Link

  1. #1
    Join Date
    Nov 2011
    Posts
    82

    Active State Same Nav Link

    I am trying to keep a "work" nav active when you go to multiple pages within my portfolio. Here is a working link of my site that I am trying to get this to work on. If you click the "work" nav you will see a secondary nav and if you click "print" the nav link at the top goes away.

    http://www.andyrichin.com/dev

    Here is the code I am using:

    PHP Code:
    <?
    $path 
    $_SERVER['PHP_SELF'];
    $page basename($path);
    $page basename($path'.php');
    ?>

    <li><a <? if($page == 'work_all') print ' class="active"'?> href="work_all.php">WORK</a></li>
    Hope this makes sense. Is there a way to have the $page == more than one identifier? This is the only solution I can think of, but doesn't work. Thanks in advance!

  2. #2
    Join Date
    Nov 2002
    Posts
    2,632
    1. You could use a switch statement and check for each case of the sub sections.

    PHP Code:
    if = home {
        
    // set main menu with class
    }
    if = 
    work {
        
    // set main menu with class
        
    checksubmenu($page);
    }
    if = 
    about {
        
    // set main menu with class
    }

    function 
    checksubmenu($page) {

    switch(
    $page) {
        case: 
    'print'
            
    // print link
            
    break;
        case: 
    'web'
            
    // print link
            
    break;
        case: 
    'branding'
            
    // print link
            
    break;
        case: default
            
    // print link for all
        
    }

    2. The subsections could be passed via a variable, and you just check for it to see which additional link gets that class as well.

    PHP Code:
    if $_GET['section'] = "print"
        
    // print link
    else if $_GET['section'] = "web"
        
    // print link
    else if $_GET['section'] = "branding"
        
    // print link
    else if $_GET['section'] = "print"
        
    // print link
    else
        
    // print link 
    Coding is crude but you get the idea.

  3. #3
    Join Date
    Nov 2011
    Posts
    82
    Ok I suck at writing PHP, could you explain more specifically?

  4. #4
    Join Date
    Nov 2002
    Posts
    2,632
    http://php.net/manual/en/control-structures.switch.php

    That should cover the switch statement. Do you understand functions or do you need help there?

  5. #5
    Join Date
    Nov 2011
    Posts
    82
    I'm still a bit confused on how this all works. Could I simply do this? Here is the code I posted before..

    Code:
    <? 
    $path = $_SERVER['PHP_SELF']; 
    $page = basename($path); 
    $page = basename($path, '.php'); 
    ?> 
    
    <li><a <? if($page == 'work_all') print ' class="active"'; ?> href="work_all.php">WORK</a></li>
    If I had all of my "work" pages, including the work index page plus all sub pages, could I say

    Code:
    <li><a <? if($page == 'work/') print 'class="active"';?>
    href="work_all.php">WORK</a><li>
    Would this work? By giving the page identifier that folder instead of a specific file?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles