views:

117

answers:

3

I am using php includes to limit redundancy on my pages, how can I have my navigation have the current page selected with say a certain color for the HOME button when my navigation is called from a header.php file.

If i were to say to add a "active" class to the home.php item and add a style so it looked different, that would happen across the board for all my pages, which is why I am using includes in the first place, how can I have one header.php file with my navigation, that also allows each page the ability to show the current page you are on reflected in the navigation?

This is the nav portion of the header.php file

  <ul>
 <li><a href="index.php">About Us</a></li>
 <li><a href="portfolio.php">Portfolio</a></li>
 <li><a href="news.php">News</a></li>
 <li><a href="contact.php">Contact</a></li>
    </ul>

This is the index.php file that the header.php file is included in

  <?php
   include("includes/header.php");
  ?>

 <div class="span-8" id="welcome">
  <p>Lorem ipsum</p>
 </div>

 <div class="span-16 last" id="slideshow">
  <img src="images/introImage1.png" alt="fountain">
  <img src="images/introImage2.png" alt="bench">
  <img src="images/introImage3.png" alt="bridge">
 </div>

 <?php
  include("includes/footer.php");
  ?>
+1  A: 

Try this:

<ul>
<?php 
$pages = array( 'index.php' => 'About Us', 'portfolio.php' => 'Portfolio', 'news.php' => 'News', 'contact.php' => 'Contact' );

foreach( $pages as $url => $title ) {
   $li = '<li ';
   if( $_SERVER[ 'PHP_SELF' ] == $url ) {
       $li .= 'class="active"';
   }
   $li .= '><a href="' . $url . '">' . $title . '</a></li>';
   echo $li;
}

?>
</ul>
Jacob Relkin
this seems to make sense, however the class active is never added to any of the pages when I view them, I am not sure what could be wrong, I am copying and pasting your code.
Anders Kitson
A: 

Another option would be to set an attribute against the body tag, and then use a matched pair to change the styling of any number of elements.

So, on your main (home) page, you may have:

<body id="home">
<a href="/" class="home">Home</a>
<a href="/about.htm" class="about">About Us</a>

Then, within your CSS have:

body#home a.home , body#about a.about {
  color:#999;background-color:#000; }
Lucanos
I am not sure how this will work because my body is withing the header.php file so I cant have separate body ids they will all be the same.
Anders Kitson
In that case, try my other suggested solution for this question.
Lucanos
A: 

And, yet another option would be to include a single CSS statement inside the actual page.

<style type="text/css">
  a[href="<?php echo basename( $_SERVER['PHP_SELF'] ); ?>"] ,
  a[href="<?php echo $_SERVER['PHP_SELF']; ?>"] {
  /* Styles for Current Page Links */ }
</style>

Of course, that is dependent on the browser being able to use that CSS Selector.

And, if you want to be completely sure that all links to the file are covered (including full URIs), then also include the following selector:

a[href="http<?php
  echo ( $_SERVER['HTTPS'] ? 's' : '' ).'://'.
    $_SERVER['HTTP_HOST'].(
      ( $_SERVER['HTTPS'] && $_SERVER['SERVER_PORT']!=443 )
      || ( !$_SERVER['HTTPS'] && $_SERVER['SERVER_PORT']!=80 ) ?
      ':'.$_SERVER['SERVER_PORT'] : '' ).
    $_SERVER['PHP_SELF']; ?>"]

UPDATED: Corrected PHP Variables to use.

Lucanos
this selector doesnt seem to work for me the location i get for the href is this /scatliffRedo/index.php do i have to prepend the web address or something so its www.somthing.com/scatliffRedo/index.php for the selector to work
Anders Kitson
Ah, my mistake. Whatever is within the quotation marks of the selector needs to be present in the links you want styled to indicate that they are the current page being viewed.So if your CSS contained `a[href*="about.php"]` then that would match any links which contained *about.php* in their href attribute. I have to admit I have not used this method previously, but I am sure with (minor) hacking it should work. I will actually start experimenting myself to find a bulletproof solution.
Lucanos
I have updated the style declarations - they should work properly now.
Lucanos