Jump to content


Photo

if..elseif...is there a if or?


  • Please log in to reply
7 replies to this topic

#1 piznac

piznac
  • Members
  • PipPipPip
  • Advanced Member
  • 261 posts

Posted 18 August 2006 - 03:26 PM

Hello all,

I have this statement:
if ($i <= $k) {
echo "<br>image1'";
}
elseif ($i <= $l){
echo "<br>image2";
}
else {
echo "<br>image3";
}

Lets say for now:
$i = 50
$k = 86
$l = 85

Now with that stated both the first two conditions ring true. My problem is it will only display the first image. I assume this is due to the elseif statement beacause it did meet the first condition. What I want to happen is if both the first conditions are true then only display the 2nd image. Because even though both are true the 2nd one at that is the only one that matters at this point. BTW this for a job completion ratio, so if your behind by 14 points  you would get a yellow image, if you are behind by 15 or more you will get a red image. But the way I have it written, if the first condition is true it stops there and displays the first image. This is not what I want. I hope someone can understand this :)

So is there a way to do this?


#2 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 18 August 2006 - 03:37 PM

What you want to do is situate your condition in a certain order.

if ($i <= $k) {
echo "<br>image1'";
}
elseif ($i <= $l && $i > $k){
echo "<br>image2";
}
else {
echo "<br>image3";
}

Ray


#3 piznac

piznac
  • Members
  • PipPipPip
  • Advanced Member
  • 261 posts

Posted 18 August 2006 - 03:42 PM

That dosent seem to work,.. I think with that "elseif" statement it stops the code right there. Because if condition1 is met there is no "else",.. right?

#4 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 18 August 2006 - 03:47 PM

OK then situate it like so

if ($i <= $k && $i < $l) {
echo "<br>image2";
}
elseif ($i <= $k){
echo "<br>image1";
}
else {
echo "<br>image3";
}[code]

Ray[/code]


#5 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 18 August 2006 - 03:50 PM

<?php

   switch (TRUE){
       case ($i <= $k):
           echo "<br>image1";

       case ($i <= $l):
           echo "<br>image2";
           break;
                                       
       default:
           echo "<br>image3";
           break;
   }

?>


#6 piznac

piznac
  • Members
  • PipPipPip
  • Advanced Member
  • 261 posts

Posted 18 August 2006 - 03:54 PM

craygo,

Now I see what you are saying,.. very good thanks.

And HeyRay2,

Is this a more efficient way to do that?

#7 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 18 August 2006 - 04:25 PM

Don't use elseif in the second statement, use if :)
I'm just a guy who likes to code for fun...

#8 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 18 August 2006 - 04:38 PM

<?php

   switch (TRUE){
       case ($i <= $k):
           echo "<br>image1";

       case ($i <= $l):
           echo "<br>image2";
           break;
                                       
       default:
           echo "<br>image3";
           break;
   }

?>

Depending on your goal, switch() can be more efficient. It basically allows you to check a variable against multiple values or conditions.

The switch block I provided you will match all cases that evaluate to "TRUE", and perform an action under those matching cases.

The break lines tell the switch() to stop executing, and no other cases will be checked. I left out the break in the first case so that the first and second case would always be checked. If neither are matched, then the default case executes.

There is more than one way to accomplish what you are trying to do, I just prefer the switch() method when I know I might need to match more than one conditional.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users