Short Version: How can I highlight the current page in the nav menu when using a php include file?
Full Version: I have a website where I'm using a navmenu.php file to generate the nav menu via a php include statement. I need to have the currently viewed page marked in the nav bar with a top and bottom border. What's the best way to do this while continuing to use the php include file?
At first I thought if using if/elseif statements, but my best idea so far is to create a switch statement in PHP to search the url and then adjust accordingly. Is that possible? If so, how can I pull the page name? For example, if the url is www.domain.com/contact.php I want to pull 'contact'
<?php
$page = [file name];
switch ($page) {
case "services.php":
//Add the border to the services page <li>
break;
case "about.php":
//Add the border to the about us page <li>
break;
case "contact.php":
//Add the border to the contactpage <li>
break;
default:
//Add the border to the home page <li>
}
?>
Based on this question, it seems like I should be able to use
switch($_GET['????'])
Thanks in advance for any help!
Added: Here's the code from the php file:
<?php
session_start();
echo "<nav class='nav-wrap'>
<ul class='navLeft'>
<li><a href='http://www.domain.com' title='Home'>HOME</a></li>
<li><a href='http://www.domain.com/about.php' title='About Us'>ABOUT US</a></li>
<li><a href='http://www.domain.com/portfolio.php' title='View our Portfolio'>PORTFOLIO</a></li>
</ul>
<ul class='navRight'>
<li><a href='http://www.domain.com/giving.php' title='Giving Back'>GIVING BACK</a></li>
<li><a href='http://www.domain.com/services.php' title='Services'>SERVICES</a></li>
<li><a href='http://www.domain.com/contact.php' title='Contact Us'>CONTACT US</a></li>
</ul>
</nav>";
?>
2nd Edit: Thank you @anujnehra, that part is starting to make sense. I've been trying to determine how to proceed once the switch identifies the current page. I'm thinking using a hidden css class and changing it to be displayed might work best, but I'm not sure how to control the css within the PHP switch.
Here's what I have so far:
CSS:
.currentPage {
border-bottom: 2px solid #ffffff;
border-top: 2px solid #ffffff;
display: none;
}
PHP File:
print_r(pathinfo("www.domain.com/contact.php"));
Array (
[dirname] => www.domain.com
[basename] => contact.php
[extension] => php
[filename] => contact
);
switch(filename){
case "about":
.currentPage#about {display:inline;}
break;
case "portfolio":
.currentPage#portfolio {display:inline;}
break;
case "giving":
.currentPage#giving {display:inline;}
break;
case "services":
.currentPage#services {display:inline;}
break;
case "contact":
.currentPage#contact {display:inline;}
break;
default:
.currentPage#home {display:inline;}
break;
}
echo "<nav class='nav-wrap'>
<ul class='navLeft'>
<li><a href='http://www.domain.com' title='Home' class='currentPage' id='home'>HOME</a></li>
<li><a href='http://www.domain.com/about.php' title='About Us' class='currentPage' id='about'>ABOUT US</a></li>
<li><a href='http://www.domain.com/portfolio.php' title='View our Portfolio' class='currentPage' id='portfolio'>PORTFOLIO</a></li>
</ul>
<ul class='navRight'>
<li><a href='http://www.domain.com/giving.php' title='Giving Back' class='currentPage' id='giving'>GIVING BACK</a></li>
<li><a href='http://www.domain.com/services.php' title='Services' class='currentPage' id='services'>SERVICES</a></li>
<li><a href='http://www.domain.com/contact.php' title='Contact Us' class='currentPage' id='contact'>CONTACT US</a></li>
</ul>
</nav>";
?>