Welcome to WebmasterWorld Guest from 54.145.85.22

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

If / Else (Simple)

I should know this...

     
7:34 pm on Sep 1, 2008 (gmt 0)

Full Member

10+ Year Member

joined:Sept 22, 2003
posts: 225
votes: 2


Looking to round out this code. I am swapping images based on /directory. How do I address the home / index page?

Example

<?if(strpos($_SERVER['PHP_SELF'],"/products/")!==false){?>
<img src="img/image1.gif" alt="" width="160" height="100" border="0">
<?}?>
<?if(strpos($_SERVER['PHP_SELF'],"/services/")!==false){?>
<img src="img/image2.gif" alt="" width="160" height="100" border="0">
<?}?>
<?if(strpos($_SERVER['PHP_SELF'],"/contact/")!==false){?>
<img src="img/image3.gif" alt="" width="160" height="100" border="0">
<?}?>
<?else>
<img src="img/image4.gif" alt="" width="160" height="100" border="0">
<?}?>

It's the last part that doesn't fly...

Thanks!
~ John

7:40 pm on Sept 1, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:May 6, 2005
posts:863
votes: 0


Just a guess
<?else> Missing open curly
<img src="img/image4.gif" alt="" width="160" height="100" border="0">
<?}?>
9:24 pm on Sept 1, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:June 6, 2005
posts:109
votes: 0


Just a guess

You're halfway there :)

<?else> is missing the open curly + closing question mark.

John - Are the second and third ifs meant to be elseifs? Also, you could combine the closing bracket with the next if to cut down on the number of php tags you're using. It might make the code a bit more readable. And it's best if you can to avoid using short tags (with 'php' written) cause they're not always supported.

<?php if(strpos($_SERVER['PHP_SELF'],"/products/")!==false){?>
<img src="img/image1.gif" alt="" width="160" height="100" border="0">
<?php }elseif(strpos($_SERVER['PHP_SELF'],"/services/")!==false){?>
<img src="img/image2.gif" alt="" width="160" height="100" border="0">

7:19 am on Sept 2, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 24, 2005
posts:697
votes: 0


Hey,

Why to make your life tough when you can write much simpler code than that, it will also help you debug easier. Personally if i wrote that i would write

<?php
if(strpos($_SERVER['PHP_SELF'],"/products/")!==false)
{
echo '<img src="img/image1.gif" alt="" width="160" height="100" border="0">';
}
elseif(strpos($_SERVER['PHP_SELF'],"/services/")!==false)
{
echo '<img src="img/image2.gif" alt="" width="160" height="100" border="0">';
}
elseif(strpos($_SERVER['PHP_SELF'],"/contact/")!==false)
{
echo '<img src="img/image3.gif" alt="" width="160" height="100" border="0">';
}
else
{
echo '<img src="img/image4.gif" alt="" width="160" height="100" border="0">';
}
?>

Hope it helps

8:34 am on Sept 2, 2008 (gmt 0)

Full Member

10+ Year Member

joined:July 12, 2002
posts:207
votes: 1


Code provided by Anyango, are much better. Your script will take less execution time with this code.
12:38 pm on Sept 2, 2008 (gmt 0)

Full Member

10+ Year Member

joined:Sept 22, 2003
posts:225
votes: 2


Works like a charm!

Thanks ~ John