www.webdeveloper.com
Page 2 of 2 FirstFirst 12
Results 16 to 25 of 25

Thread: Highlight Current Page Using PHP

  1. #16
    Join Date
    Nov 2011
    Posts
    82
    Ok that one works! Here is the link to the site I'm building where I'm trying to incorporate this code. www.andyrichin.com/work_all.php

    As you can see in the nav up top right corner is the main nav I'm working with. How would you manipulate your code so that the Work link stays lit even when you go to say Creative Juice, or any of them for that matter? Instead of echo <li> which seems to place it up in the nav.

    Does is have anything to do with class="active" vs class="current"? Thanks for the help!

  2. #17
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by andy.richin View Post
    Ok that one works! Here is the link to the site I'm building where I'm trying to incorporate this code. www.andyrichin.com/work_all.php

    As you can see in the nav up top right corner is the main nav I'm working with. How would you manipulate your code so that the Work link stays lit even when you go to say Creative Juice, or any of them for that matter? Instead of echo <li> which seems to place it up in the nav.

    Does is have anything to do with class="active" vs class="current"? Thanks for the help!
    You could add an OR into the condition checked. Something like:
    PHP Code:
    (strpos($_SERVER['PHP_SELF'], $page) > || strpos($_SERVER['PHP_SELF'], "Work" ) > 0

  3. #18
    Join Date
    Nov 2011
    Posts
    82
    So would that replaces the current echo strpos($_SERVER..... line? Is there any way to accomplish this same thing without embedding it in the unordered list and just calling it in the list? If that makes sense.

  4. #19
    Join Date
    Jan 2009
    Posts
    3,346
    I'm not sure what you mean. Are you wanting a different HTML output?

  5. #20
    Join Date
    Nov 2011
    Posts
    82
    I just want to have 1 header file.. 3 nav links (Home, Work and About), and have the Work link stay hilighted (.current) when you visit the subpages in Work such as Print, Web, Packaging. Is this possible? This is crazy difficult for some reason. I do appreciate all of the help, and maybe I am just missing something.

  6. #21
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by andy.richin View Post
    I just want to have 1 header file.. 3 nav links (Home, Work and About), and have the Work link stay hilighted (.current) when you visit the subpages in Work such as Print, Web, Packaging. Is this possible? This is crazy difficult for some reason. I do appreciate all of the help, and maybe I am just missing something.
    Sounds a little more complicated. Might need a nested array that way.

    PHP Code:
    $aboutPages = Array('Home''Work'=>Array('Print','Web','Packaging'),'About'); 
    Then you'll need to check if the "current" menu is an Array in which case you'll run a second loop.
    PHP Code:
       echo "<li>"
       echo 
    "<a href=\''$page\""
       echo (
    strpos($_SERVER['PHP_SELF'], $page) > 0) ? " class=\"current\""''
       echo 
    ">$page</a>";
       if(
    is_array($page)){
          echo 
    "<ul>";
          foreach(
    $page as $subPage){
             echo 
    "<li>"
             echo 
    "<a href=\''$page\""
             echo (
    strpos($_SERVER['PHP_SELF'], $subPage) > 0) ? " class=\"current\""''
             echo 
    ">$subPage</a>"
             echo 
    "</li>"
          }
          echo 
    "</ul>";
       }
       echo 
    "</li>"
    Untested of course...might need adjustment.

  7. #22
    Join Date
    Jan 2009
    Posts
    3,346
    You would probably also need to adjust your css so any "parent" pages would also highlight if any children were highlighted.

  8. #23
    Join Date
    Nov 2011
    Posts
    82
    So if I used the above PHP code you provided, would there be any PHP in the each list item.. something like this?

    HTML Code:
    <li><a <?php if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="active"';?> href="index.php">HOME</a></li>
    And then change the 'index.php'?

  9. #24
    Join Date
    Jan 2009
    Posts
    3,346
    Sounds like you might need to restructure the data again if your displayed label isn't the same as the URL for the href.

    Something like:
    PHP Code:
    $aboutPages = Array(Array('label'=>'Home''href'=>'index.php'),
                                 Array(
    'label=>'Work', 'href'=>'work.php', 'subMenus'=>Array(Array('label'=>'Print','href'=>'print.php'),Array('label'=>'Web','href'=>'web.php'),Array('label'=>'Packaging','href'=>'packaging.php')),
                                Array('
    label'=>'About','href'=>'about.php')); 
    Then instead of is_array, you'll need to check if that 'subMenus' index exists.

    PHP Code:
       if(isset($page['subMenus'])){ 
          echo 
    "<ul>"
          foreach(
    $page['subMenus'] as $subPage){ 
    And your echos would change to reference by name the attribute you are wanting
    PHP Code:
       echo "<a href=\''".$page['href']."\""
       echo (
    strpos($_SERVER['PHP_SELF'], $page['href']) > 0) ? " class=\"current\""''
       echo 
    ">".$page['label']."</a>"
    All code just proof of concept and untested of course. May need adjustment to run.
    Last edited by criterion9; 07-24-2012 at 11:32 AM.

  10. #25
    Join Date
    Nov 2011
    Posts
    82

    resolved

    Thanks for the help man! I came across a really simply tutorial that worked and made sense to me.

    http://buildinternet.com/2009/12/usi...mplate-faster/

    Really easy. Pretty much just uses page titles. Thanks again!

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