dpuk44 Posted February 15, 2017 Share Posted February 15, 2017 I have my menu in an include file called menu.php and I want to assign a class called 'active' to the <li> to the page that I am on. How can I do this in PHP? <!-- navigation --> <nav class="navbar navbar-default" role="navigation"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#collapse"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> </div> <div class="collapse navbar-collapse" id="collapse"> <ul class="nav navbar-nav navbar-right"> <li><a href="index.php">Home</a></li> <li><a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Insect Control <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="insect-control.php">Insect Control</a></li> <li><a href="ant-cockroach-control.php">Ant & Cockroach Control</a></li> <li><a href="bed-bug-treatment.php">Bed Bug Treatments</a></li> <li><a href="fly-control.php">Fly Control</a></li> <li><a href="wasp-hornets-bee-control.php">Wasp, Hornet & Bee Control</a></li> </ul> </li> <li><a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Rodent & Vermin <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="rodent-vermin.php">Rodent & Vermin</a></li> <li><a href="rats-mice-infestation.php">Rats & Mice Infestation</a></li> <li><a href="squirrel-control.php">Squirrel Control</a></li> </ul> <li><a href="#">Contact</a></li> </ul> </div> </nav> <!-- end navigation --> Quote Link to comment https://forums.phpfreaks.com/topic/303200-ahhh-how-to-assign-active-class-to-menu-item/ Share on other sites More sharing options...
Jacques1 Posted February 15, 2017 Share Posted February 15, 2017 $current_script = basename($_SERVER['SCRIPT_FILENAME']); gives you the filename of the current script. 1 Quote Link to comment https://forums.phpfreaks.com/topic/303200-ahhh-how-to-assign-active-class-to-menu-item/#findComment-1542792 Share on other sites More sharing options...
gizmola Posted February 15, 2017 Share Posted February 15, 2017 This seemingly simple question actually calls into question a number of things you are doing that aren't great from a security or SEO standpoint. While Jacques1 handed you a technique to identify the currently requested parent script in a web server environment, your code reveals some things you are doing that could be better handled explicit with a better architecture pattern. There are many out there, but the most typically use are front-controller/mvc. For example, if you had a front controller script, you would already know where you were in the control structure all the time, and that state could be passed into the menu. In most apps these days, people are moving to a richer client experience with more javascript client code/ ajax etc., and less dependency on page requests. It is also much better to obfuscate your underlying architecture for SEO purposes, not to mention the fact it lessens what others know about your hosting environment. Google likes this: yoursite.com/fly-control Much better than yoursite.com/fly-control.php Your site is easier to maintain as well if you are using a front controller that all requests are going through. With the proliferation of small easy to use micro frameworks like slim, silex, lumen etc. or even symfony3 configured as a microkernel, the answer to your question could be very different if you improved your underlying site architecture. For reference: http://symfony.com/blog/new-in-symfony-2-8-symfony-as-a-microframework 1 Quote Link to comment https://forums.phpfreaks.com/topic/303200-ahhh-how-to-assign-active-class-to-menu-item/#findComment-1542804 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.