Forum Moderators: coopster

Message Too Old, No Replies

Multiple IF Statement Problem

         

kevape

1:13 am on Jan 29, 2008 (gmt 0)

10+ Year Member



Hello, You can probably see what i want to do, i want to display a certain image when the variable contains a certain value. and the 5 star image when 5.00 is reached, for some reason, even though the variable $StarRating has a value of E.G. 3.09 or 1.00 the picture that gets displayed is "4star.jpg". Can anybody see my problem, Thanks Guys/Gals.

if (($StarRating =1) && ($StarRating <=1.99)){

$StarPicture = "1star.jpg";

if (($StarRating =2) && ($StarRating <=2.99)){

$StarPicture = "2star.jpg";

if (($StarRating =3) && ($StarRating <=3.99)){

$StarPicture = "3star.jpg";

if (($StarRating =4) && ($StarRating <=4.99)){

$StarPicture = "4star.jpg";

}else{

$StarPicture = "5star.jpg";

}
}
}
}

FourDegreez

1:38 am on Jan 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I assume those equals signs are meant to be greater than or equal, correct? Also, instead of nesting the if statements, just make every one but the first an else if.

vincevincevince

1:41 am on Jan 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$StarPicture=(($StarRating>5)?("5"):(floor($StarRating)))."star.jpg";

[edited by: eelixduppy at 1:41 am (utc) on Jan. 29, 2008]
[edited: vincevincevince]<case correction>

PHP_Chimp

11:53 am on Jan 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Vince's method above is the most compact way of doing what you want, however if you find that hard to read then you may want to use switch.

$star_rating = [url=http://uk2.php.net/manual/en/function.floor.php]floor[/url]($StarRating); // so 2.7 = 2. You may want [url=http://uk2.php.net/manual/en/function.ceil.php]ceil[/url] so that 2.7 = 3.
switch ($star_rating) {
case 1:
$StarPicture = "1star.jpg";
break
case 2:
$StarPicture = "2star.jpg";
break
case 3:
$StarPicture = "3star.jpg";
break
case 4:
$StarPicture = "4star.jpg";
break
case 5:
$StarPicture = "5star.jpg";
break;
default: // for testing, as this shouldnt ever be called
echo 'Something is wrong';
break;
}

Although with
$StarPicture=(($StarRating>5)?("5"):(floor($StarRating)))."star.jpg";

You will only get 5 stars if the rating is above 5. So I guess you need
 $StarPicture = (($StarRating == 5)?("5"):(floor($StarRating)))."star.jpg";

kevape

1:51 pm on Jan 29, 2008 (gmt 0)

10+ Year Member



Thank you for the input guys, this is the best way for me to read it.
But i cant get it to work, im getting the echo "something is wrong" and no image is diplaying, i have the the &StarRating is currently 3.09 in this case, so i would expect to hav "3star.jpg" to diplay but no image is found?

Sorry, im new!

$star_rating = floor($StarRating); // so 2.7 = 2. You may want ceil so that 2.7 = 3.
switch ($star_rating) {
case 1:
$StarPicture = "1star.jpg";
break
case 2:
$StarPicture = "2star.jpg";
break
case 3:
$StarPicture = "3star.jpg";
break
case 4:
$StarPicture = "4star.jpg";
break
case 5:
$StarPicture = "5star.jpg";
break;
default: // for testing, as this shouldnt ever be called
echo 'Something is wrong';
break;
}

PHP_Chimp

4:16 pm on Jan 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you echo $StarRating do you get the answer that you expect?

Actually scrap that...You need to put; at the end of the breaks, as I forgot them. Sorry.


$star_rating = floor($StarRating); // so 2.7 = 2. You may want ceil so that 2.7 = 3.
switch ($star_rating) {
case 1:
$StarPicture = "1star.jpg";
break;
case 2:
$StarPicture = "2star.jpg";
break;
case 3:
$StarPicture = "3star.jpg";
break;
case 4:
$StarPicture = "4star.jpg";
break;
case 5:
$StarPicture = "5star.jpg";
break;
default: // for testing, as this shouldnt ever be called
echo 'Something is wrong';
break;
}

That should work better. ;)

<edit>
You may be able to use $StarRating and be able to remove the $star_rating. I just put that other variable in there if you needed the original unaltered $StarRating later in your script.

[edited by: PHP_Chimp at 4:18 pm (utc) on Jan. 29, 2008]