homepage Welcome to WebmasterWorld Guest from 54.163.72.86
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
If / Else (Simple)
I should know this...
King of Bling




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

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




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

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

MattAU




msg:3736162
 9:24 pm on Sep 1, 2008 (gmt 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">

Anyango




msg:3736358
 7:19 am on Sep 2, 2008 (gmt 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

Dinkar




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

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

King of Bling




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

Works like a charm!

Thanks ~ John

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved