FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Problem with PHP if else statements.





The-Master
Well I have my code, which I spent a while working. I had it working perfectly until I put the else statement at the end:

Code:
<?php

$img = @$_GET['image'];

if ( $img == "Dougies Rugby Team in P3.gif" ) {
    echo " <img src=\"http://www.dougie.logical-host.com/photos/$img\" width=\"100%\" height=\"100%\"/><br/>";
echo "<body bgcolor=\"#FFFFFF\"><div align=\"center\"><iframe src=\"http://www.dougie.logical-host.com/tagbox1/\" width=\"210\" height=\"400\" frameborder=\"0\" border=\"0\"></iframe></div>";
}


if ( $img == "Dougies Rugby Team in P3.gif2" ) {
    echo " <img src=\"http://www.dougie.logical-host.com/photos/$img\" width=\"100%\" height=\"100%\"/><br/>";
echo "<body bgcolor=\"#FFFFFF\"><div align=\"center\"><iframe src=\"http://www.dougie.logical-host.com/tagbox2/\" width=\"210\" height=\"400\" frameborder=\"0\" border=\"0\"></iframe></div>";
}


else {
    echo "<img src=\"http://www.dougie.logical-host.com/photos/$img\" width=\"100%\" height=\"100%\"/><br/>";
echo "<body bgcolor=\"#FFFFFF\"><div align=\"center\"><iframe src=\"http://www.dougie.logical-host.com/tagbox2/\" width=\"210\" height=\"400\" frameborder=\"0\" border=\"0\"></iframe></div>";
}

?>


So that works, but why when I have $img being "Dougies Rugby Team in P3.gif" why does the if and else statements both appear? It is very strange. Even stranger is that when I have $img being "Dougies Rugby Team in P3.gif2" only that if statement appears (this is what I want!)

I am sorry if that was difficult to understand. If you can understand a bit just ask and I will attempt to explain more clearly.
ninjakannon
I can see the problem and it's a simple one.

When $img equals "Dougies Rugby Team in P3.gif2" it ignores the first if statement and then runs the 2nd if statement, it is true and so runs the if.

When $img equals "Dougies Rugby Team in P3.gif" the first if statement is true so that runs. Then it looks at the 2nd if statement, the initial statement is not true, so it runs the else section.

Do you see what's going wrong here? I hope I've outlined it clearly.

All you need to do is change the 2nd if to else.
So this line:
Code:
if ( $img == "Dougies Rugby Team in P3.gif2" ) {

becomes this:
Code:
else ( $img == "Dougies Rugby Team in P3.gif2" ) {

This will ensure that only one statement is run, simple really. Wink
hexkid
Code:
if ($img == ".gif") {
  // do ".gif" thing
}

if ($img == ".gif2") {
  // do ".gif2" thing
} else {
  // do NOT ".gif2" thing
  ### AND ".gif" IS NOT a ".gif2"
}
ninjakannon
Umm... hexkid, won't the code you've suggested still cause the same problem?

If $img equals ".gif" then the first if statement will run through and so will the else statement. All you appear to have suggested is that you should change what $img is equal to and therefore change the strings that you have to test them against. How will this fix the problem?

Sorry if I've missed something here, but I really don't see the improvement in what you've suggested. Confused
hexkid
It's not an improvement; it's an explanation of why it happens.
Try this for an improvement:
Code:
if ($img == ".gif") {
  // do ".gif" thing
} else if ($img == ".gif2") {
  // do ".gif2" thing
} else if ($img == ".gif3") {
  // do ".gif3" thing
} else {
  // do thing for anything different
}


[Edit]or even this
Code:
switch ($extension) {
  case ".gif":
    // do ".gif" thing
    break;
  case ".gif2":
    // do ".gif2" thing
    break;
  default:
    // do thing for anything different
    break;
}
blueray
The-Master wrote:
Well I have my code, which I spent a while working. I had it working perfectly until I put the else statement at the end:

<?php

Quote:
$img = @$_GET['image'];

Part 1:
if ( $img == "Dougies Rugby Team in P3.gif" ) {
echo " <img src=\"http://www.dougie.logical-host.com/photos/$img\" width=\"100%\" height=\"100%\"/><br/>";
echo "<body bgcolor=\"#FFFFFF\"><div align=\"center\"><iframe src=\"http://www.dougie.logical-host.com/tagbox1/\" width=\"210\" height=\"400\" frameborder=\"0\" border=\"0\"></iframe></div>";
}



Part 2:
if ( $img == "Dougies Rugby Team in P3.gif2" )
{
echo " <img src=\"http://www.dougie.logical-host.com/photos/$img\" width=\"100%\" height=\"100%\"/><br/>";
echo "<body bgcolor=\"#FFFFFF\"><div align=\"center\"><iframe src=\"http://www.dougie.logical-host.com/tagbox2/\" width=\"210\" height=\"400\" frameborder=\"0\" border=\"0\"></iframe></div>";
}
else
{
echo "<img src=\"http://www.dougie.logical-host.com/photos/$img\" width=\"100%\" height=\"100%\"/><br/>";
echo "<body bgcolor=\"#FFFFFF\"><div align=\"center\"><iframe src=\"http://www.dougie.logical-host.com/tagbox2/\" width=\"210\" height=\"400\" frameborder=\"0\" border=\"0\"></iframe></div>";
}


?>


So that works, but why when I have $img being "Dougies Rugby Team in P3.gif" why does the if and else statements both appear? It is very strange. Even stranger is that when I have $img being "Dougies Rugby Team in P3.gif2" only that if statement appears (this is what I want!)

I am sorry if that was difficult to understand. If you can understand a bit just ask and I will attempt to explain more clearly.


This should be easier to find what's the issues ? right ? Smile
ninjakannon
Oh I see, sorry hexkid.

Yeah, you've basically suggested the same as I did, although we both used different methods of explanation.

Your problem is solved now, The-Master! Smile

EDIT:
I hadn't thought of the 2nd suggestion you made, hexkid. It doesn't matter which is used though, really. All The-Master needs to do is change one of his ifs to an else and his code will work.

Oh and blueray, I don't see the point in posting that - other than to increase your points (which you don't need to do anyway).
The-Master
Thanks so much ninjakannon and hexkid Smile
Related topics
htaccess Password Protection
PHP Tutorials
Suggestion -> A small one though
is artificial intelligence old?
Some php probs..need some help plzz!
php dropdown lists with if statements or switch
php: if else or somethink else
if() and else(): Help needed!
I'm doing something wrong,,,
Layout Gap PRoblems with PHP/Javascript Combo
Stats problem - php execution problem (MOD Assist Required)
Screen resoltion detection script not working??
phpBB3 UCP Profile Problem
About downtime and new server replacing Server 2/3
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.