Jump to content

[SOLVED] Help setting data in header file from a seperate file


rickphp

Recommended Posts

Hi,

 

This may sound a little confusing but I will explain as best as I can!

 

I am using includes to build up my pages, for example the contact page would be made up of the following includes; header.tpl, contact.tpl and footer.tpl

 

What I am trying to do is make it so that when I navigate between pages the relative navigation button will be set to "active" in the header.tpl file, so that the user can see what page they are on when they look at the navigation buttons. If this isn't possible then any other way of acheiving the same outcome is fine. But this has got me a little stuck.

 

So if I was on the home page the header would be something like this (I've removed a lot of div tags etc to make things as simple as possible for any one who can help).

 

<a href="home" title="" class="active">Home</a><a href="example" title="">Example</a><a href="contact" title="">Contact</a>

 

If i then navigated to the contact page the header should look like this:

 

<a href="home" title="">Home</a><a href="example" title="">Example</a><a href="contact" title="" class="active">Contact</a>

 

Hopefully you guys will be able to help! Thanks in advance for anyone who can help!

well you can do

$url = $_SERVER["REQUEST_URI"];

that will give you the page you are on (for example if at www.mysite.com/news.html that will return news.html)

 

and you can do a switch statement to decide which gets the class

Hi,

I would suggest that you make an array of menu items like this:

$menu=array();
$menu[home.php]="Home";
$menu["example.php"]="Example";
$menu["contact.php"]="Contact"
etc.

Then loop through the array to create the html:

foreach($menu AS $url=>$txt){
  echo '<a href="'.$url.'"';
  if($url==$_SERVER["REQUEST_URI"]) echo ' class="active"';
  echo '>'.$txt.'</a>';
}

 

Not tested that code but it should work.

 

Chris

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.