Forum Moderators: coopster

Message Too Old, No Replies

Trouble with if/else statements in navigation

Can't seem to specify what page the navigation is on

         

polyxena

1:23 am on Aug 18, 2006 (gmt 0)

10+ Year Member



At the top of each page on the site, I have this:

<? $navlocation = 'page';?>

where page is a name for whatever page I'm on. So if it's the home page, for instance, I put

<? $navlocation = 'home';?>

So the idea here is, if it's the home page, display a static image; otherwise, display the rollover image.

To accomplish this, I have created a menu.php file that has all the site's navigation with if/else statements; I am including this on all the pages in the site. Here's what the code for the home page link looks like:

<? if($navlocation == 'home') {?>
<img src="images/home-inactive.jpg" alt="Home" width="195" height="36" border="0" />
<? } else {?>
<a href="http://www.somesite.com/"
onmouseover="changeImages('home', 'images/home-over.jpg'); return true;"
onmouseout="changeImages('home', 'images/home.jpg'); return true;"><img src="images/home.jpg" alt="Home" name="home" width="195" height="36" border="0" id="home" /></a>
<? }?>

Unfortunately, this doesn't work. If I put the string <? $navlocation = 'home';?> in the menu.php file it works fine, but that negates the whole point.

So what am I doing wrong here?

A while back I did this sort of thing a different way where the if/else statements were focused on the actual file path and directories involved, but I've forgotten what the right string is for that.

I'd really appreciate some help with this!

[edited by: dreamcatcher at 11:58 am (utc) on Aug. 18, 2006]
[edit reason]
[1][edit reason] No urls as per TOS [webmasterworld.com] Thanks. [/edit]
[/edit][/1]

sonjay

1:40 am on Aug 18, 2006 (gmt 0)

10+ Year Member



I have to ask the obvious question: Does the $navlocation = 'page' statement appear in your code before the include statement? Because if the include statement appears first, the variable definition won't be available to the included file.

In your included menu.php page, what happens if you put the line echo $navlocation?

polyxena

2:01 am on Aug 18, 2006 (gmt 0)

10+ Year Member



Yes, the navlocation variable is at the top of the page, well before the include.

So I tried your suggestion in a couple of ways...

first I tried putting

<? echo $navlocation;?>

at the top of the menu.php file.

Then I tried adding it right before the if statement:

<? echo $navlocation; if($navlocation == 'home') {?>

I don't know much about PHP, really, so I could have done that wrong too... but neither way worked... thanks for replying, though. Do I need to go about this a different way?

supermoi

4:06 am on Aug 18, 2006 (gmt 0)

10+ Year Member



If the echo doesn't work, it's because the variable is either undefined, empty or unaccessible.
Make sure the script that's doing the condition has access to the variable (ie, it's not inside a function, or the variable wasn't defined inside a function). Also make sure that you don't redefine the variable somewhere between the first declaration and the condition.

polyxena

5:19 am on Aug 18, 2006 (gmt 0)

10+ Year Member



Well, this is the thing. I genuinely cannot figure out why the variable isn't available.

I put

<? echo $navlocation;?>

in the actual html file, which is what contains the variable, to see if it would work and it didn't!

Is there something wrong with this?

<? $navlocation = 'home';?>

Does it need to go in a specific point on the page, or can it go anywhere before the include? I've tried it in all kinds of places (the top of the page before the head, just after the body tag, and in the same table cell as the include) and it doesn't seem to make a difference.

The include works fine:

<? include 'menu.php'?>

I also tried

<? $navlocation = 'home';
include 'menu.php'?>

but the results were the same. It just seems like the navlocation variable string is being completely ignored, but I can't figure out why. This is in an HTML file; I added a MIME type so the server knows to parse HTML files for PHP, and like I said, the include works fine. I'm so confused!

leadegroot

11:39 am on Aug 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does the
<? if($navlocation == 'home') {?> 

appear inside a function?
If so, the $navlocation is "not in scope" - the function can't see it.
make it global and it will be able to see it

(but I doubt you are using a function - tell us more about what you are doing...)

sonjay

11:53 am on Aug 18, 2006 (gmt 0)

10+ Year Member



Try putting this on your html page (anywhere is fine):
<?php
$navlocation = 'home';
echo 'NAVLOCATION: '.$navlocation;
?>

And see if your $navlocation gets echoed out.

Also, just a FYI: It's generally better to use the full <?php opening tag rather than the shorthand <? one -- that shorthand one doesn't work on all servers, and if you ever do any xhtml page it can much up the doctype.

polyxena

1:21 am on Aug 19, 2006 (gmt 0)

10+ Year Member



Thank you for the help and your responses... I am going on vacation for 7 days and I don't have time to test your suggestions but I will when I get back and let you know what happens. :)

polyxena

1:31 am on Aug 26, 2006 (gmt 0)

10+ Year Member



Thanks for the suggestions from last week; I have returned from vacation and today I tested the most recent suggestion.

First I tried:

<?php
$navlocation = 'home';
echo 'NAVLOCATION: '.$navlocation;
?>

and this caused, as expected,

navlocation: home

to appear at the top of the page.

So next I tried this:

<?php $navlocation = 'home';?>
<?php include 'http://www.somesite.com/menu.php';?>

Then put this in the menu.php file:

<?php echo 'navlocation: '.$navlocation;?>
<?php if($navlocation == 'home') {?>
<img src="http://www.somesite.com/images/home-inactive.jpg" alt="Home" width="195" height="36" border="0" />
<?php } else {?>
<a href="http://www.somesite.com/"
onmouseover="changeImages('home', 'http://www.somesite.com/images/home-over.jpg'); return true;"
onmouseout="changeImages('home', 'http://www.somesite.com/images/home.jpg'); return true;">
<img src="http://www.somesite.com/images/home.jpg" alt="Home" name="home" width="195" height="36" border="0" id="home" /></a>
<?php }?>

When I did that, the navlocation did NOT echo. I got:

navlocation:

Why would this be? I'm so confused.

[edited by: dreamcatcher at 6:30 am (utc) on Aug. 26, 2006]
[edit reason]
[1][edit reason] No urls as per TOS [webmasterworld.com] Thanks. [/edit]
[/edit][/1]

GMassi

2:09 am on Aug 26, 2006 (gmt 0)

10+ Year Member



Use a local pathname not a URL in your include statement.

Assuming that menu.php and the other pages that need to include it are in the same directory

<?php include 'menu.php';?>

should solve your problem.

polyxena

3:46 am on Aug 26, 2006 (gmt 0)

10+ Year Member



That fixed it! Thank you SO much.