Forum Moderators: coopster
$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
$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)
casestatements 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
$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"; $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"; 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