Jump to content

Identifying the same current page with a number of pages


barrycorrigan

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.