SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with using selected class with includes...

    Hi guys,

    I'm using PHP includes for my site and in the header include I have my main navigation - so it's drawn in to whatever page the user goes to from header.php.

    I want to have a 'selected' <li> class for the navigation so the relevant button is highlighted depending on which page/section the user is on...

    How would I go about implementing it into header.php - is there a way I can put a bit of PHP in each of the pages to make the appropiate <li> in header.php use the 'selected' class?

    Thank you and I hope to hear from you.

    SM

  2. #2
    Life is not a malfunction gold trophysilver trophybronze trophy
    TechnoBear's Avatar
    Join Date
    Jun 2011
    Location
    Argyll, Scotland
    Posts
    6,444
    Mentioned
    274 Post(s)
    Tagged
    5 Thread(s)
    You mean something like this?
    Code:
    <?php $currentPage =  basename($_SERVER['SCRIPT_NAME']);
    ini_set('display_errors', false); 
    ?>
    
    <div id="nav">
    <ul>
    <li <?php if ($currentPage == 'index.php') {echo 'class="current"';} ?>><a href="index.php">Home</a></li>
    <li <?php if ($currentPage == 'foo.php') {echo 'class="current"';} ?>><a href="design.php">Foo</a></li>
    <li <?php if ($currentPage == 'bar.php') {echo 'class="current"';} ?>><a href="standards.php">Bar</a></li>
    <li <?php if ($currentPage == 'contact.php') {echo 'class="current"';} ?>><a href="contact.php">Contact Me</a></li>
    </ul>	
    </div><!-- End nav -->

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, exactly that!!!

    Thank you so much, top shooter!!

    SM

  4. #4
    Life is not a malfunction gold trophysilver trophybronze trophy
    TechnoBear's Avatar
    Join Date
    Jun 2011
    Location
    Argyll, Scotland
    Posts
    6,444
    Mentioned
    274 Post(s)
    Tagged
    5 Thread(s)
    Great, glad I could help. As this turned out to be a PHP issue, rather than a CSS one, I'll move the thread to the PHP forum, for the benefit of anybody else with a similar question.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •