Forum Moderators: coopster

Message Too Old, No Replies

PHP Include question...

         

wfernley

6:37 pm on Jan 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello. I am designing a site using PHP. I have my main page (index.php) with a navigation bar at the top using a PHP include <?php include('includes/topnav.php');?>. I wanted to do something special with the buttons in topnav.php. When the user is on one of the buttons it takes them to the corresponding page. I want the buttons to change different colors depending on the page the user is on. If the user was on the Home page, I would like the Home button to be a different color, of if the user was on that About page, I would like the About button to be a different color. An example of this is on the APC website, even though this is a common look on sites. Is there a line of code in php that will allow topnav.php to know which actual page the user is on and highlight the corresponding button.

Thanks in advance for the help. Hope it made sense. :)

Wes

Romeo

7:34 pm on Jan 7, 2004 (gmt 0)

10+ Year Member



Hi Wes,

the name of the page can be seen in the PHP's environment variable $_SERVER["PHP_SELF"].
So you may try and put a
(if $_SERVER["PHP_SELF"] == "/about.php") {
echo "this is the about page";
}
else {
echo "this is not the about page";
}
right in your topnav.php to know what page you are on and did the include.

HTH and regards,
R.

wfernley

8:08 pm on Jan 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the post. I will try this out later today :)

Wes

bsterz

8:31 pm on Jan 7, 2004 (gmt 0)

10+ Year Member



I think this is kinda cool. It loops thru all the images and looks at the link the image points to - if the current page is that link, it loads the "Over" image. It basically swaps the image for an image with the same name with "Over" appended.

Please be gentle..it's not an example of advanced scripting :) just something I cooked up in a pinch.

Is just calls the mouseover function that comes with Dreamweaver. I kinda hate it, but keep using it on all kinds of sites...I think I'm a masochist

function SetMouseOver(){
for(i=0;i<document.images.length;i++){
//If the link attached to the image matches the current document location..
if (document.images[i].parentElement == document.location.href){
CurImg = document.images[i].name
FullImagePath = new Array();
ImageName = new Array();
//Get the image name in an array
FullImageName = document.images[i].src.split("/");
ImageName = FullImageName.pop(); //Put the last element in ImageaName and modify FullImageName
RealImagePath = FullImageName.join("/"); //Now..put the slashes back in the image path
ImageName = ImageName.split(".");
JustImageName = RealImagePath+'/'+ImageName[0];
ImageExtension = ImageName[1];
MM_swapImage(document.images[i].name,'',JustImageName+'Over.'+ImageExtension,1);
//alert(RealImagePath+' '+JustImageName+' '+ImageExtension)
}
}
}

HTH

Bill

wfernley

9:09 pm on Jan 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Bsterz.

Where would I put that script? The topnav.php doesn't have a head tag because its an include so I can't put it in there.

Wes

coopster

9:23 pm on Jan 7, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You wouldn't even need javascript. One neat trick is to use CSS and put a class on the BODY tag dynamically based on the selection:

<?php
$tab = basename($_SERVER['PHP_SELF'], ".php");
$body_attributes = '';
if ($tab) $body_attributes = "class=\"$tab\"";
?>
<body<?php if ($body_attributes) print " $body_attributes";?>>
<div id="topnav">
<a href="tab1.php" id="tab1" title="Tab1">tab1</a>
<b>¦</b><a href="tab2.php" id="tab2" title="Tab2">tab2</a>
<b>¦</b><a href="tab3.php" id="tab3" title="Tab3">tab3</a>
<b>¦</b><a href="tab4.php" id="tab4" title="Tab4">tab4</a>
</div>

and include something like this in your stylesheet:

<style>
body {background-color: #CCC; }
#topnav a:hover, #topnav a:active {background-color: #603; color: #FFC; }
.tab1 #topnav #tab1 {color: #FFC; background: transparent;}
.tab2 a#tab2, .tab3 a#tab3, .tab4 a#tab4 {color: #FFC; background: transparent;}
</style>

Yeah, I know the colors are terrible -- you take it from here ;)

bsterz

9:32 pm on Jan 7, 2004 (gmt 0)

10+ Year Member



I would add it to a .js file for the site

wfernley

9:50 pm on Jan 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ahhh I never thought of that. :) Thanks.

Thanks for the post Coopster, Im going to look into that too. I can see there are many ways to do this.

Thanks for the help guys :)

Wes

wfernley

2:06 pm on Jan 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, Well after fooling around with it last night, I made this up:

<?php
if ($_SERVER["PHP_SELF"]== "index.php") {
?>
<a href="index.php"><img src="images/home-over.gif" alt="index.php" width="49" height="23" border="0"></a>
<?php
} else {
?>
<a href="index.php"><img src="images/home.gif" alt="index.php" width="49" height="23" border="0"></a>
<?php
}
?>