Jump to content

Recommended Posts

I have a site with the nav being divs .. and based on if the user is on that page, I want that tab to be a different color.

 

Whats the best way to do this using php ?

 

http://74.55.14.226/~green/solarpower.php

 

you can see that the tab for the solar page is green ... but i need that to change based on what page you are on ...

 

here is my code :

 

<div class='navlinkpush'>

<div class='navlinkactive'><a href='solarpower.php'>solar</a></div>			      
<div class='navlink'><a href='windpower.php'>wind</a></div>			      
<div class='navlink'><a href='nuclearpower.php'>nuclear</a></div>			      
<div class='navlink'><a href='biofuel.php'>bio</a></div>			      
<div class='navlink'><a href='hybridcars.php'>hybrids</a></div>			      
<div class='navlink'><a href='hydrogenpower.php'>hydrogen</a></div>			      
<div class='navlink'><a href='about.php'>about</a></div>			      
<div class='navlink'><a href='blog.php'>blog</a></div>

</div> <!-- #navlinkpush -->	

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/131795-solved-navigation-help/
Share on other sites

this should work...

 

just continue the elseif statements for each link/page you have and complete $arr.

 

should do the job.

 

<?php
$getArr = explode(".", $_SERVER['REQUEST_URI']);
if(strpos($getArr[0], "/solarpower")){
      $link = "solar";
}elseif(strpos($getArr[0], "/windpower")){
      $link = "wind";
}
$arr = array("solar","wind");
foreach($arr as $key => $value){
      echo '<div'.($value==$link ? ' style="navlinkactive"' : ' style="navlink"').'>'.$value.'</div>';
}
?>

create script navbar.php

<?php
$navbar = array(
'solar' => 'solarpower.php',
'wind' => 'windpower.php',
'nuclear' => 'nuclearpower.php',
'bio' => 'biofuel.php',
'hibrids' => 'hybridcars.php',
'hydrogen' => 'hydrogenpower.php',
'about' => 'about.php',
'blog' => 'blog.php'
);
$a = explode('/',$_SERVER['SCRIPT_NAME']);
$activ_link = array_pop($a);
foreach ($navbar as $text => $link){
$class = $activ_link == $link ? 'navlinkactive' : 'navlink';
echo "<div class='$class'><a href='$link'>$text</a></div>\n";
}
?>

and include it in your scripts

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.