Skip to content Skip to sidebar Skip to footer

Hilighting The Current Navigation Tab Using Url In Php

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['SC

Solution 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.

Solution 2:

Hope it helps

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

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

   foreach($nav_tabsas$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;
}

Post a Comment for "Hilighting The Current Navigation Tab Using Url In Php"