barrycorrigan Posted April 8, 2011 Share Posted April 8, 2011 Hi everyone, I have this script that identify's the current page in the navigation <?php $currentPage = basename($_SERVER['SCRIPT_NAME']); ?> <ul id="nav"><!-- Open Nav --> <li><a href="index2.php" <?php if ($currentPage == 'index2.php') {echo 'class="selected"';} ?>>Home</a></li> <li><a href="salon-information.php" <?php if ($currentPage == 'salon-information.php') {echo 'class="selected"';} ?>>Salon Information</a></li> <li><a href="about-us.php" <?php if ($currentPage == 'about-us.php') {echo 'class="selected"';} ?>>About us</a></li> <li><a href="hair-care-advice.php" <?php if ($currentPage == 'hair-care-advice.php') {echo 'class="selected"';} ?>>Hair Care Advice</a></li> </ul><!-- Close Nav --> My problem is that hair-care-advice.php has a number of sub pages. How do I get it so the selected class remains selected for all the sub pages as well as the main page. Any help would be greatly appreciated Thank you Barry Quote Link to comment https://forums.phpfreaks.com/topic/233068-identifying-the-same-current-page-with-a-number-of-pages/ Share on other sites More sharing options...
Adam Posted April 8, 2011 Share Posted April 8, 2011 Unless they all have a common string within the URL (e.g. 'hair-care-advice.php' or 'hair-care-advice/sub-page.php'), you can't do it based on the URL. If they do of course, you can just check the request URI server variable instead, and try to match the shared string. Your other option would be to set a variable within each page that includes the navigation, and define the selected page that way. For example (and guessing of course what your code is like): hair-care-advice.php: [...] $currentPage = 'hair-care-advice'; require_once 'navigation.php'; [...] navigation.php: <ul id="nav"><!-- Open Nav --> <li><a href="index2.php" <?php if ($currentPage == 'index2') {echo 'class="selected"';} ?>>Home</a></li> <li><a href="salon-information.php" <?php if ($currentPage == 'salon-information') {echo 'class="selected"';} ?>>Salon Information</a></li> <li><a href="about-us.php" <?php if ($currentPage == 'about-us') {echo 'class="selected"';} ?>>About us</a></li> <li><a href="hair-care-advice.php" <?php if ($currentPage == 'hair-care-advice') {echo 'class="selected"';} ?>>Hair Care Advice</a></li> </ul><!-- Close Nav --> Quote Link to comment https://forums.phpfreaks.com/topic/233068-identifying-the-same-current-page-with-a-number-of-pages/#findComment-1198663 Share on other sites More sharing options...
barrycorrigan Posted April 8, 2011 Author Share Posted April 8, 2011 Hi MrAdam, That worked a treat... Thanks Quote Link to comment https://forums.phpfreaks.com/topic/233068-identifying-the-same-current-page-with-a-number-of-pages/#findComment-1198672 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.