Forum Moderators: coopster

Message Too Old, No Replies

Switch / Case not working for me

         

wheelie34

7:35 pm on Apr 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can anyone point me in the right direction? I added the print line at the end to make sure I had a value for $average_score, and I have, the correct figure gets printed out.

$average_score = sprintf("%0.1f", "$rating");
switch (TRUE){
case "$average_score >1 && <2"; #1 star
echo "<img src=\"1star.jpg\">";
break;
case "$average_score >2 && <3"; #2 star
echo "<img src=\"2star.jpg\">";
break;
case "$average_score >3 && <4"; #3 star
echo "<img src=\"3star.jpg\">";
break;
}
print "$average_score";

if my value is 3.5 I still get the 1star.jpg where have I gone wrong, thanks for looking

adb64

8:11 pm on Apr 23, 2006 (gmt 0)

10+ Year Member



I wouldn't use a switch statement in this case but go for an if else if construct.


$average_score = sprintf("%0.1f", "$rating");
if (($rating >= 1) && ($rating < 2))
{
echo "<img src=\"1star.jpg\">";
}
else
if ($rating < 3)
{
echo "<img src=\"2star.jpg\">";
}
else
if ($rating < 4)
{
echo "<img src=\"3star.jpg\">";
}
print "$average_score";

Also your cases didn't take into account when a value was exactly 2 or 3, no case would match in that case.
Also $average_score >1 && <2 will not work it should be something like ($average_score > 1) && ($average_score < 2)

coopster

9:15 pm on Apr 23, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The
case
statements should be followed by a colon, not a semicolon. And the reason your control structure is grabbing the first one every time is because your case statement is true, not only for the first case, but all cases -- you have them cast as a string, not a comparison construct. You need parentheses, not double quotation marks. I may be repeating what adb64 is stating but wanted to offer a bit more clarification. Note also how the logic must be written, it cannot be used like an SQL BETWEEN function:
($average_score > 1 && < 2) // wrong 
($average_score > 1 && $average_score < 2) // right

You have to specify the variable again or you are going to get a parse error. Here is how you should have written it out originally:
$average_score = sprintf("%0.1f", 3.5); 
switch (TRUE)
{
case ($average_score > 1 && $average_score < 2): #1 star
echo "<img src=\"1star.jpg\">";
break;
case ($average_score > 2 && $average_score < 3): #2 star
echo "<img src=\"2star.jpg\">";
break;
case ($average_score > 3 && $average_score < 4): #3 star
echo "<img src=\"3star.jpg\">";
break;
}
print "$average_score";

Now, as adb64 said, you could use an if statement, but with a little bit of thought you can apply your logic and get that switch to make a bit more sense and lot more flexible for future updates:
$average_score = sprintf("%0.1f", 3.5); 
switch ($average_score)
{
case ($average_score > 4): # > 4 = unknown star
echo "<img src=\"unknownstar.jpg\">";
break;
case ($average_score > 3): #3 star
echo "<img src=\"3star.jpg\">";
break;
case ($average_score > 2): #2 star
echo "<img src=\"2star.jpg\">";
break;
case ($average_score > 1): #1 star
echo "<img src=\"1star.jpg\">";
break;
default: # < 1 = unknown star
echo "<img src=\"unknownstar.jpg\">";
}
print "$average_score";

Note the addition of a default or fallback in the event we go higher than any expected value or lower than any expected value where we don't meet any of the $average_score criteria we have defined.

wheelie34

7:45 am on Apr 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



thank you both, I have now learnt or understand 2 more things in php

1) I thought else and elseif couldnt be used that many times

2) I did think it was getting accepted on every option and noted how coops has started high and ended lower, makes sense now.

I used case initially for future changes as coops said

thanks again guys