1

I am creating menu, and if the specific page is current , menu item assign different color. Simple situation. So i do :

<?php $page = $_SERVER['SCRIPT_NAME'];?>
<?php if ($page == "index.php"){ echo "class='active'";} ?> >
 <a href="index.php">Home</a> 

That works for every page, except one. I tried some other options suggested on stackoverflow

How to get current PHP page name , like :

echo basename($_SERVER['PHP_SELF']); returns file_name.php

and

if(basename(__FILE__) == 'file_name.php') {
  //Hide
} else {
  //show
 }

Still does not work.

Community
  • 1
  • 1
kaulainais
  • 115
  • 1
  • 2
  • 12

3 Answers3

0
$pagename = basename($_SERVER['PHP_SELF']);
if($pagename=='index.php'){
    echo "class='active'";
}
Grant
  • 2,413
  • 2
  • 30
  • 41
0

According to the data you supplied I suggest to use solution instead of solution. I mean that you use .

This is may be done as follows:

<ul id="menu">
  <li><a href="index.php">Home</a></li>
  <li><a href="about.php">About</a></li>
  <li><a href="any/other/path/index.php">Any Other</a></li>
</ul>

<script>
function setCurrentNavLink(id){
  nav = document.getElementById(id);
  links = nav.getElementsByTagName('a');
  for (i = 0; i < links.length; i++){
    if (links[i].href == location.href){         
      links[i].className = 'current';
      break;
    }
  }

}

  setCurrentNavLink('menu');
</script> 

Check this demo: http://jsbin.com/tomid/1

SaidbakR
  • 13,303
  • 20
  • 101
  • 195
0

Sorted. The problem was in JQuery , thats operates Tabs on the current page.

  $(document).ready(function(){

    // When a link is clicked
    $("a.tab").click(function () {


        // switch all tabs off
        $("ul.tabs li a.active ").removeClass("active");
                    //$(".active ").removeClass("active");

        // switch this tab on
        $(this).addClass("active");
        //$(".collapse navbar-collapse").addClass("active");


        // slide all content up
        $(".content").slideUp();

        // slide this content up
        var content_show = $(this).attr("title");
        $("#"+content_show).slideDown();

    });

  });

The mistake was in 3rd section, where Class .active was removed from Every object. Including header. Therefore header did not appear as active. Instead of that, class .active had to be removed only from Tabs tab.

kaulainais
  • 115
  • 1
  • 2
  • 12