Welcome to WebmasterWorld Guest from 54.160.163.163

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

If / Else (Simple)

I should know this...

     

King of Bling

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

10+ Year Member



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

wheelie34

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

WebmasterWorld Senior Member 10+ Year Member



Just a guess
<?else> Missing open curly
<img src="img/image4.gif" alt="" width="160" height="100" border="0">
<?}?>

MattAU

9:24 pm on Sep 1, 2008 (gmt 0)

10+ Year Member



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">

Anyango

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

WebmasterWorld Senior Member 10+ Year Member



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

Dinkar

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

10+ Year Member



Code provided by Anyango, are much better. Your script will take less execution time with this code.

King of Bling

12:38 pm on Sep 2, 2008 (gmt 0)

10+ Year Member



Works like a charm!

Thanks ~ John

 

Featured Threads

Hot Threads This Week

Hot Threads This Month