Hilighting The Current Navigation Tab Using URL in

2019-02-28 07:53发布

问题:

Hilighing the current navigation tab using php url ,with and with out .php Extension

 //php code
function curPageName() {
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
$Current=curPageName();;
?> 

//HTML,CSS

<li id=<?php if ("index.php"==$Current) echo "selected";else echo "";?>>

回答1:

Instead, I use something like this to get current page as active menu in navigation bar:

  • Put $page = "page_name" in your pages, for example for home page, let it be $page = "home"
  • Now in a common file, where your navigation bar's HTML resides, it should look something like this:

<li <?php echo ($page == 'home') ? "class='active'" : ""; ?> title="Home Page"><a href="<?php echo BASE_URL; ?>">Home</a></li>
<li <?php echo ($page == 'article') ? "class='active'" : ""; ?> title="Recent articles"><a href="<?php echo BASE_URL; ?>/articles">Articles</a></li>
...
...

  • Style active class using CSS to achieve desired effect.


回答2:

Hope it helps

<?php

    function curPageName() {
    return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
    }
    $Current=curPageName();

   $nav_tabs=array('home','about','products','contacts');

   foreach($nav_tabs as $nav)
   {
          if($nav == $current)
         {
             echo"<li class='active' > <a href='$nav' > $nav </a></li>";
         }
         else
        {
             echo"<li  > <a href='$nav' > $nav </a></li>";
        }
   }
?>

// in CSS define active to highlight the tab

.active{
background-color:black;
font-white;
}