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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.