Forum Moderators: coopster

Message Too Old, No Replies

Using if-else statement in navigation

need help using php if-else statements in my navigation

         

Spyce

7:32 am on Aug 18, 2010 (gmt 0)

10+ Year Member



First, a link to my site (this isn't the permanent URL.. this is just where I have the content stored while I work on it): [vanilla-chai.nu ].

Second, the issue: I'm not very good with PHP coding.. I know enough so that I can make page includes on my sites, and that's it. What I'm looking for is this.. when a person clicks on any of the navigation links, I want the image to stay on the hover state. For example, when a person clicks "contact", I want the "contact" link to remain underlined while they are on the contact page. (Note: my navigation is currently set-up using CSS images/hover states). Basically, I want something that says if page = blah blah blah, then display image-underline.jpg else display image.jpg.

How might I go about doing this?

PS: Like I said, I'm not good with PHP at all, so you'll have to walk me through this as if I'm 2.

londrum

9:24 am on Aug 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



you could try something like this

if ($_SERVER['SCRIPT_NAME']=='/directory/page.php') {

 // user is on /directory/page.php

} else {

 // user is not on /directory/page.php

}

Spyce

12:59 pm on Aug 18, 2010 (gmt 0)

10+ Year Member



That's very similar to what I'd like, but I don't know how to code it.

if ($_SERVER['SCRIPT_NAME']=='/directory/page.php') {
// user is on /directory/page.php
} else {
// user is not on /directory/page.php
}

how do I word that?

if ($_SERVER['$page']=='/directory/page.php') {
display imageHover.jpg
} else {
display image.jpg
}

I tried this and it's not working. I don't know how to make the images display.

londrum

1:23 pm on Aug 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



you dont have to change the $_SERVER['SCRIPT_NAME'] bit. thats what its supposed to say. the bit you change is /directory/page.php
you have to change that to whatever the URL of your page is.
to display the image you'd do something like this

if ($_SERVER['SCRIPT_NAME']=='/directory/page.php') {

echo '<img src="imageHover.jpg">';

} else {

echo '<img src="image.jpg">';

} 

Spyce

1:32 pm on Aug 18, 2010 (gmt 0)

10+ Year Member



I was fooling around and attempted to write my own code, which I think I did fairly well considering my lack of knowledge, however I still can't get it to work:


<a href="index.php"><?php

$page = '/index.php';

if ($page = '/index.php') {
print ("<img src=images/hnav-home.jpg>");
}
elseif ($page = '/about.php') {
print ("<img src=images/nav-home.jpg>");
}
elseif ($page = '/resume.php') {
print ("<img src=images/nav-home.jpg>");
}
elseif ($page = '/portfolio.php') {
print ("<img src=images/nav-home.jpg>");
}
elseif ($page = '/contact.php') {
print ("<img src=images/nav-home.jpg>");
}
else {
print ("img src=images/nav-home.jpg>");
}

?></a>


and also this one I tested on my about.php link:


<a href="?page=about"><?php

$page = '/about.php';

if ($page = '/about.php') {
print ("<img src=images/hnav-about.jpg>");
}
else {
print ("img src=images/nav-about.jpg>");
}

?>


If you go to my site here [vanilla-chai.nu], you'll see that when the page loads, both the 'home' and 'about' links are underlined. If you click any of the other links (contact, portfolio, etc) the home and about buttons still remain underlined. This is when I wrote the [first snippet of] code, because I wanted a certain image to display if you were on home.php, and a different one to display if you were on about.php, contact.php, etc.

I know I'm headed in the right direction, I just need some guidance getting there :-p I appreciate your patience and help :)

londrum

1:38 pm on Aug 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



you probably just need a double equals sign for the if statement.

so it should be

if ($page == '/about.php') {

instead of

if ($page = '/about.php') {


p.s... using $_SERVER['SCRIPT_NAME'] is just another way of doing your $page variable. but it sets the value all by itself. so you don't have to bother writing out a value for $page at the top of all your scripts.

Spyce

2:08 pm on Aug 18, 2010 (gmt 0)

10+ Year Member



I tried both methods- first I tried making my single '=' into a '==', and when I did that, no image showed up at all.

So I switched and used the '$_SERVER['SCRIPT_NAME']' you suggested and still no image shows up:

<a href="?page=about"><?php

if ($_SERVER['SCRIPT_NAME']=='/about.php') {
print ("<img src=images/hnav-about.jpg>");
}
else {
print ("img src=images/nav-about.jpg>");
}

?></a>


I played with both codes and discovered that using '==' instead of just '=', is what causes the image to not show up. So I changed the '==' back to '=' in both examples, and still nothing. the link remains underlined no matter what page I go to.

londrum

3:17 pm on Aug 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



you can't swap the equal signs. they mean two completely different things

= means 'is equal to'
== means 'IF it is equal to'

if you are using the IF statement then only the double equals sign will work.

what we are trying to do is check whether $_SERVER['SCRIPT_NAME'] is equal to the specified URL.

but this is where it is going wrong. because we are not checking for the same thing. it looks like what you actually want to check is the query string on the end of the script's name (?page=about)

look at your code.
your page's URL seems to be
?page=about --- which presumably means the full URL is /index.php?page=about
but you're checking whether its equal to
/about.php

so the answer is going to be no, its not the same.

that means it is going to print out the second option. but it looks like you have a typo. you have put
print ("img src=images/nav-about.jpg>");
instead of
print ("<img src='images/nav-about.jpg'>");

(you have missed off the opening bracket). that is probably why no image appeared last time.

if you are checking for query strings (which is what ?page=about is), then forget $_SERVER['SCRIPT_NAME']. you need to use $_GET['page'] instead.
try this

if ($_GET['page']=='about') {
print ("<img src='images/hnav-about.jpg'>");
}
else {
print ("<img src='images/nav-about.jpg'>");
}

jspeed

4:59 pm on Aug 18, 2010 (gmt 0)

10+ Year Member



I use the same type of code in my navigation. I just echo the css class that I mimics the hover state.

$page = $_REQUEST[ 'page' ];
if ($page=="page1")
echo "<li class='currentnavitem'>";
else echo "<li>"; echo "<a href='?page=page1'>Page1</a></li>";

Spyce

3:30 am on Aug 19, 2010 (gmt 0)

10+ Year Member



I want to thank you for all your help. I got this working perfectly on all but one of the links.... the 'home' link.

I've looked over and over and over this, and I can't seem to figure out why it's not working; it's the exact same code, just cut 'n' pasted, changing the parts I need to change to correspond with the link- but nothing. It just displays the standard non-underline image.

If you want to try and play with the code, I uploaded a .rar file [vanilla-chai.nu] with all my site's content as it currently stands. My apologies for not being too organized; I haven't really gotten to the organization stage yet. I just wanted to get things working first- then tackle the rest later :-p

Spyce

3:35 am on Aug 19, 2010 (gmt 0)

10+ Year Member



Scratch that. I fixed it. Not sure what the issue was, but I re-pasted everything and now it works perfectly.

Thank you again for all your help! And thank you for being so patient with me and my ignorance! haha

Matthew1980

7:27 am on Aug 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

Glad you got it fixed in the end Spyce, have fun with the rest of the project now.

Jspeed:

$page = $_POST['page'];
if ($page=="page1"){
echo "<li class='currentnavitem'>";
}
else{
echo "<li>"; echo "<a href='?page=page1'>Page1</a></li>";
}


Try not to use $_REQUEST other than for development, because there are inherent security vulnerabilities with that - I assume as you would need to use $_POST for this example, but the smae logic applies for $_GET array. Also it's a good idea to use the braces for structuring the if/else conditional statements, purely for ease of reading. Just thought I should mention it.

Cheers,
MRb