Merlin
Merlin

Reputation: 199

Rating a user based on his score, load certain image if the score is between certain interval

I want to load a rating image which depends on the users average score on some tests. For example If the users score is between 5(not including 5 ) and 6 (including 6), he should get image 6.png, which is a 3 star rating.

So far I tried this, but it doesnt work, it seems there is a mistake somewhere.

Rating: 
<? if ($row['AVG(answer)'] < 1): ?><img src='images/rating/0.png' hspace='3'   style='vertical-align:middle;'><? endif;?> 
<? if ($row['AVG(answer)'] == 1): ?><img src='images/rating/1.png' hspace='3' style='vertical-align:middle;'><? endif;?>
<? if ($row['AVG(answer)'] > 1 && <= 2): ?><img src='images/rating/2.png' hspace='3' style='vertical-align:middle;'><? endif;?>
<? if ($row['AVG(answer)'] > 2 && <= 3): ?><img src='images/rating/3.png' hspace='3' style='vertical-align:middle;'><? endif;?>
<? if ($row['AVG(answer)'] > 3 && <= 4): ?><img src='images/rating/4.png' hspace='3' style='vertical-align:middle;'><? endif;?>
<? if ($row['AVG(answer)'] > 4 && <= 5): ?><img src='images/rating/5.png' hspace='3' style='vertical-align:middle;'><? endif;?>
<? if ($row['AVG(answer)'] > 5 && <= 6): ?><img src='images/rating/6.png' hspace='3' style='vertical-align:middle;'><? endif;?>
<? if ($row['AVG(answer)'] > 6 && <= 7): ?><img src='images/rating/7.png' hspace='3' style='vertical-align:middle;'><? endif;?>
<? if ($row['AVG(answer)'] > 7 && <= 8): ?><img src='images/rating/8.png' hspace='3' style='vertical-align:middle;'><? endif;?>
<? if ($row['AVG(answer)'] > 8 && <= 9): ?><img src='images/rating/9.png' hspace='3' style='vertical-align:middle;'><? endif;?>
<? if ($row['AVG(answer)'] > 9 && <= 10): ?><img src='images/rating/10.png' hspace='3' style='vertical-align:middle;'><? endif;?>

any ideas anyone?

Upvotes: 0

Views: 87

Answers (3)

Raffael Luthiger
Raffael Luthiger

Reputation: 2211

The answers from the others are good to. But here is the real problem of your code:

You wrote:

if ($row['AVG(answer)'] > 3 && <= 4)

but correct is:

if ($row['AVG(answer)'] > 3 && $row['AVG(answer)'] <= 4)

Upvotes: 1

user1909426
user1909426

Reputation: 1668

If $row['AVG(answer)'] can only be 0 to 9 then why not just do

<img src='images/rating/<?php echo ceil($row['AVG(answer)']); ?>.png' hspace='3' style='vertical-align:middle;'>

Also, there may be a error in your query (should be doing something like SELECT AVG(answer) as avg_answer ... then echoing $row['avg_answer']

Upvotes: 1

Fluffeh
Fluffeh

Reputation: 33532

You can simply use a ceil statement to make this much easier:

<?php
    echo "<img src='images/rating/".ceil($row['AVG(answer)']).".png' hspace='3'   style='vertical-align:middle;'>";
?>

This will work for all the data you have. No need for all those multiple if statements.

You only ever want things rounded up, so this will work just fine for all instances.

Upvotes: 0

Related Questions