IF Statement Problem, Help Please Anyone!

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
kevape
-


Joined: 17 Sep 2006
Posts: 2
Location: Stoke

PostPosted: Tue Jan 29, 2008 1:43 am    Post subject: IF Statement Problem, Help Please Anyone! Reply with quote

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";


}
}
}
}
_________________
K Mitchell
Back to top View user's profile Send private message
rrinc
-


Joined: 24 Feb 2006
Posts: 725
Location: Arkansas, USA

PostPosted: Tue Jan 29, 2008 2:17 am    Post subject: Reply with quote

You should try out rounding the number to make things easier: round()

Also, a single equals sign cannot be used for comparisons since its used for assigning values, use == for comparisons.

Something like:
Code:

if($StarRating <= 5) // I'm assuming your star rating can be no more than 5
{
     $StarRating = round($StarRating); // rounding it
     $StarPicture = $StarRating . 'star.jpg'; // setting the value of the picture to star.jpg preceded by the number.
}

_________________
-Blake | New Server :D
SaveTheInternet
Soy hispanohablante. Puedes contactarme por mensajes privados.
Back to top View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Tue Jan 29, 2008 11:37 am    Post subject: Re: IF Statement Problem, Help Please Anyone! Reply with quote

kevape, I can see your problem straight away, you are nesting your if statements within each other

Have another look at your code below
Code:
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";
         } //else
      } //($StarRating =3) && ($StarRating <=3.99)
   } //($StarRating =2) && ($StarRating <=2.99)
} //($StarRating =1) && ($StarRating <=1.99)

None of the other if statements will be accessed unless $StarRating is between 1 and 1.99
But because your using "=" $StarRating gets set to the number of the if statement and is always true

You should be using "==" (2 equal signs ("="), no space) to check if its equal to

Try the following
Code:
/* > greater than
 * >= greater than or equals to
 * == equals to
 * <= less than or equals to
 * < less than
 */

if( ($StarRating >= 1) && ($StarRating < 2) ){
   $StarPicture = "1star.jpg";
}elseif( ($StarRating >= 2) && ($StarRating < 3) ){
   $StarPicture = "2star.jpg";
}elseif( ($StarRating >= 3) && ($StarRating < 4) ){
   $StarPicture = "3star.jpg";
}elseif( ($StarRating >= 4) && ($StarRating < 5) ){
   $StarPicture = "4star.jpg";
}else{
   $StarPicture = "5star.jpg";
}

so even if $StarRating is 1.999999999999999999999 then it'll still display 1star.jpg
_________________
Anthony R

Roganty
| Links-Links.co.uk
Back to top View user's profile Send private message Visit poster's website
rrinc
-


Joined: 24 Feb 2006
Posts: 725
Location: Arkansas, USA

PostPosted: Wed Jan 30, 2008 12:21 am    Post subject: Reply with quote

I like my way better, gets the job done without having to do so many conditions. :P
_________________
-Blake | New Server :D
SaveTheInternet
Soy hispanohablante. Puedes contactarme por mensajes privados.
Back to top View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Wed Jan 30, 2008 10:41 pm    Post subject: Reply with quote

rrinc wrote:
I like my way better, gets the job done without having to do so many conditions. :P


True, but from his code it looks like he's rounding down the number so probably floor() would of been better

But then if he wanted to round up... ceil()
_________________
Anthony R

Roganty
| Links-Links.co.uk
Back to top View user's profile Send private message Visit poster's website
aprelium
-


Joined: 22 Mar 2002
Posts: 6800

PostPosted: Thu Jan 31, 2008 5:33 pm    Post subject: Re: IF Statement Problem, Help Please Anyone! Reply with quote

kevape,

A more concise version of rrinc's code (and with no assumption on the $StarRating value) would be:

Code:
$StarPicture = max( min( floor($StarRating), 5), 1) . "star.jpg";

_________________
Support Team
Aprelium - http://www.aprelium.com
Back to top View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group