www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 25

Thread: Highlight Current Page Using PHP

  1. #1
    Join Date
    Nov 2011
    Posts
    82

    Highlight Current Page Using PHP

    I found some code that highlights a nav link for each page that you are on this developer site http://www.elanman.co.uk/2009/01/hig...-php-includes/

    I am trying to manipulate a few things so that my "Work" nav stays highlighted for multiple pages not just work.php. Here is the code I have so far from this tutorial

    Original:
    PHP Code:
    <?php
    if (strpos($_SERVER['PHP_SELF'], 'about.php')) echo 'class="current"';
    ?>
    HTML Code:
    <ul id="navigation">
        <li><a <?php if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="current"';?> href="index.php">Home Page</a></li>
        <li><a <?php if (strpos($_SERVER['PHP_SELF'], 'about.php')) echo 'class="current"';?> href="about.php">About Me</a></li>
        <li><a <?php if (strpos($_SERVER['PHP_SELF'], 'contact.php')) echo 'class="current"';?> href="contact.php">Contact Me</a></li>
        <li><a <?php if (strpos($_SERVER['PHP_SELF'], 'work.php')) echo 'class="current"';?> href="work.php">Work</a></li>
    </ul>
    This is the code found in the same tutorial that explains how to supposedly do what I am trying to do.

    PHP Code:
    $aboutPages = array('page1.php', 'page2.php', 'page3.php', 'page4.php');

    <?php
    foreach ($aboutPages as $page) {
     if (
    strpos($_SERVER['PHP_SELF'], $page)) echo 'class="current"';
    }
    ?>
    In the HTML code above, I am not sure what to insert in the php code in the <a> tag to identify the page. I tried $page but it didn't work. Any thoughts or help?

  2. #2
    Join Date
    Jan 2004
    Posts
    484
    I am not proof positive about what you are trying to do. so this is what I think you are trying to do.


    PHP Code:
    <ul id="navigation">
    <?php 

    $aboutPages 
    = array('page1.php''page2.php''page3.php''page4.php'); 

    foreach (
    $aboutPages as $page) { 
      echo <
    li>;
      echo 
    '<a href=\''.$page.'\' ';
      (
    strpos($_SERVER['PHP_SELF'], $page))? echo 'class="current"':''
      echo 
    ' > '.$page.'</a>';
      echo </
    li>;

    ?>
    </ul>

    I didn't test but you can try the code above.

    Natdrip :P
    "water go down the hole" - plucky duck

  3. #3
    Join Date
    Nov 2011
    Posts
    82
    So you are saying put it all in the unordered list tag? When I tried that I get errors on "echo <li>;" "(strpos($_SERVER..." and the second "echo </li>;"

    All I'm really trying to do is have my Work link in my main nav stay highlighted for multiple pages, not just work.php (such as work_print.php, work_web.php, etc). Hopefully that explains it better.

  4. #4
    Join Date
    Jan 2004
    Posts
    484
    is the nav an include?
    or is it hard coded on very page?

    if it is hard coded then just add the class='current'

    and build out the css to reflect that.

    if it is included then you'll need to strip the url



    PHP Code:

        
    function ShowFileName($filepath
        { 
            
    preg_match('/[^?]*/'$filepath$matches); 
            
    $string $matches[0]; 
            
    #split the string by the literal dot in the filename 
            
    $pattern preg_split('/\./'$string, -1PREG_SPLIT_OFFSET_CAPTURE); 
            
    #get the last dot position 
            
    $lastdot $pattern[count($pattern)-1][1]; 
            
    #now extract the filename using the basename function 
            
    $filename basename(substr($string0$lastdot-1)); 
            
    #return the filename part 
            
    return $filename
        } 
    then call it
    PHP Code:
            $str $_SERVER['SCRIPT_NAME'];
            
    ShowFileName($str); 

    This should give you the file name

    Then run it through the build array with a conditional that determines which page you should high light.

    kind of what was in the first reply you may have to tweek it.
    Natdrip :P
    "water go down the hole" - plucky duck

  5. #5
    Join Date
    Jan 2009
    Posts
    3,346
    (strpos($_SERVER['PHP_SELF'], $page))? echo 'class="current"':'';
    probably meant
    PHP Code:
    (strpos($_SERVER['PHP_SELF'], $page))? echo ' class="current"':; 
    or
    PHP Code:
    (strpos($_SERVER['PHP_SELF'], $page))? echo ' class="current"':echo ''

  6. #6
    Join Date
    Nov 2011
    Posts
    82
    Yes that nav is in an include header.php file. Unfortunately I am not good at writing PHP and I am still not sure what you mean. Any example / tutorial you can point me to?

  7. #7
    Join Date
    Nov 2011
    Posts
    82
    Because unfortunately I need it spelled out for me otherwise I will not figure it out!

  8. #8
    Join Date
    Jan 2009
    Posts
    3,346
    At first glance the code NatDrip initially posted should work, just replace that line that had bad syntax like I suggested.

  9. #9
    Join Date
    Nov 2011
    Posts
    82
    So this? Inside of the <ul> tag? And I added your correction as well.

    PHP Code:
    <ul id="navigation">
    <?php 

    $aboutPages 
    = array('page1.php''page2.php''page3.php''page4.php'); 

    foreach (
    $aboutPages as $page) { 
      echo <
    li>;
      echo 
    '<a href=\''.$page.'\' ';
    (
    strpos($_SERVER['PHP_SELF'], $page))? echo ' class="current"':;   echo ' > '.$page.'</a>';
      echo </
    li>;

    ?>
    </ul>
    Do I need anything else?

  10. #10
    Join Date
    Jan 2009
    Posts
    3,346
    Did it work? I don't have an environment to test your code in...nor do I have all your code.

  11. #11
    Join Date
    Nov 2011
    Posts
    82
    I get a syntax error on lines 7, 9 and 10

  12. #12
    Join Date
    Nov 2011
    Posts
    82
    This is my original code that I was using. It works for each individual page, but not multiple pages with the same nav element.

    HTML Code:
    <body onload="displayCD()">
    
    <?php
    
    if (strpos($_SERVER['PHP_SELF'], 'about.php'));
    if (strpos($_SERVER['PHP_SELF'], 'index.php'));
    if (strpos($_SERVER['PHP_SELF'], 'work_all.php'));
    
            <div id="main_nav">
            	<ul>
                	<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="active"';?> href="index.php">HOME</a></li>
                    <li><a <?php if (strpos($_SERVER['PHP_SELF'], 'work_all.php')) echo 'class="active"';?> href="work_all.php">WORK</a></li>
                    <li><a <?php if (strpos($_SERVER['PHP_SELF'], 'about.php')) echo 'class="active"';?> href="about.php">ABOUT</a></li>
                </ul>
            </div>
    Now this is what was recommended but again, there are syntax errors on "echo <li>;", (strpos($_SERVER.....", and "echo </li>;".

    HTML Code:
    <ul id="navigation"> 
    <?php 
    
    $aboutPages = array('page1.php', 'page2.php', 'page3.php', 'page4.php'); 
    
    foreach ($aboutPages as $page) { 
    **echo <li>; 
    **echo '<a href=\''.$page.'\' '; 
    (strpos($_SERVER['PHP_SELF'], $page))? echo ' class="current"':;***echo ' > '.$page.'</a>'; 
    **echo </li>; 
    } 
    ?> 
    </ul>
    Thoughts?

  13. #13
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by andy.richin View Post

    Now this is what was recommended but again, there are syntax errors on "echo <li>;", (strpos($_SERVER.....", and "echo </li>;".

    HTML Code:
    <ul id="navigation"> 
    <?php 
    
    $aboutPages = array('page1.php', 'page2.php', 'page3.php', 'page4.php'); 
    
    foreach ($aboutPages as $page) { 
    **echo <li>; 
    **echo '<a href=\''.$page.'\' '; 
    (strpos($_SERVER['PHP_SELF'], $page))? echo ' class="current"':;***echo ' > '.$page.'</a>'; 
    **echo </li>; 
    } 
    ?> 
    </ul>
    Thoughts?
    Looks like you are missing quotes on your echo. Do you see how the syntax highlighting looks strange? Also I'm assuming your *** are marking the lines with errors and are not actually included.

    PHP Code:
    echo "<li>"

  14. #14
    Join Date
    Nov 2011
    Posts
    82
    That didn't do anything. I attached a screen shot of my code and where the errors are.
    Attached Images Attached Images

  15. #15
    Join Date
    Jan 2009
    Posts
    3,346
    PHP Code:
    <ul>
    <?php
    $aboutPages 
    = array('page1.php''page2.php''page3.php''page4.php'); 

    foreach (
    $aboutPages as $page) { 
       echo 
    "<li>"
       echo 
    "<a href=\''$page\""
       echo (
    strpos($_SERVER['PHP_SELF'], $page) > 0) ? " class=\"current\""'';
       echo 
    ">$page</a>"
       echo 
    "</li>"

    ?>
    </ul>
    Try that one.

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