Forum Moderators: coopster

Message Too Old, No Replies

PHP Include Navigation Menu

Menu that changes depending on what page is calling it

         

scott859

10:00 pm on May 24, 2006 (gmt 0)

10+ Year Member



Greetings,

I'm trying to create a PHP menu that is called as an include file. Depending on what page is displaying (calling) it, the menu will display an alternate image (different color in this case) indicating what page the user is on.

I have done this in ASP, but can't seem to get it to work in PHP - any help is greatly appreciated.

In ASP a sample for the code will look like this:

<% if page="home_page" then %>

<td><a href="index.php" onMouseOut="MM_swapImgRestore();" onMouseOver="MM_swapImage('navigation_r1_c1','','navigation/navigation_r1_c1_f2.gif',1);"><img name="navigation_r1_c1" src="navigation/navigation_r1_c1_f2.gif" width="71" height="12" border="0" alt="home page"></a></td>

<% else %>

<td><a href="index.php" onMouseOut="MM_swapImgRestore();" onMouseOver="MM_swapImage('navigation_r1_c1','','navigation/navigation_r1_c1_f2.gif',1);"><img name="navigation_r1_c1" src="navigation/navigation_r1_c1.gif" width="71" height="12" border="0" alt="home page"></a></td>"

<% end if %>

I am trying this in PHP but with no success:

<?php

if ( $page_name =="home_page" )
{
$home_navigation ="<td><a href="index.php" onMouseOut="MM_swapImgRestore();" onMouseOver="MM_swapImage('navigation_r1_c1','','navigation/navigation_r1_c1_f2.gif',1);"><img name="navigation_r1_c1" src="navigation/navigation_r1_c1_f2.gif" width="71" height="12" border="0" alt="home page"></a></td>";

}else{

$home_navigation = "<td><a href="index.php" onMouseOut="MM_swapImgRestore();" onMouseOver="MM_swapImage('navigation_r1_c1','','navigation/navigation_r1_c1_f2.gif',1);"><img name="navigation_r1_c1" src="navigation/navigation_r1_c1.gif" width="71" height="12" border="0" alt="home page"></a></td>";
}

echo $home_navigation;

?>

------------------------------------------------

I've created the following variable in the PHP home page (the home page calls the navigation include file):

<?php
$page_name = "home_page";
?>

In ASP I would do it like this on the page the called the navigation include file:

<% page_name="home_page"%>

I believe I'm creating an error when I create the variable "home_page" in the page that is calling the PHP include file but I'm not sure about this.

Thanks in advance for any help you can provide.

Scott

eelixduppy

10:13 pm on May 24, 2006 (gmt 0)



Everything looks good. Just make sure that you are including the file AFTER the variable declaration. Also, for further debugging, add error_reporting(E_ALL); to the top of the script.

One other thing. When you set a variable, you MUST escape all the quotes (replace all " with \") otherwise you are going to get errors. For more information on this look here [us3.php.net]

scott859

11:20 pm on May 24, 2006 (gmt 0)

10+ Year Member



Thanks so much! ... I wasn't escaping the " with \"

I've now escaped all double qoutes within the variable declarations and it is working fine.

Thanks again.

Scott

eelixduppy

2:53 am on May 25, 2006 (gmt 0)



You could also do it like this, which will save you some code, especially if you want to add more outcomes:

if($page == "home_page") {
$source = "navigation/navigation_r1_c1_f2.gif";
}
else {
$source = "navigation/navigation_r1_c1.gif";
}
echo "<td><a href=\"index.php\" onMouseOut=\"MM_swapImgRestore();\" onMouseOver=\"MM_swapImage('navigation_r1_c1','','navigation/navigation_r1_c1_f2.gif',1);\"><img name=\"navigation_r1_c1\" src=\"".$source."\" width=\"71\" height=\"12\" border=\"0\" alt=\"home page\"></a></td>";

scott859

2:58 pm on May 25, 2006 (gmt 0)

10+ Year Member



Yes, that's good to know - that will save some coding.

Thanks again.

Scott

frozenpeas

11:43 pm on May 27, 2006 (gmt 0)

10+ Year Member



If you have to use a lot of else if statements i.e. you have a lot of pages in the nav.

Use switch instead