Question

So im trying to create a highscore table for a "create your own burger" application.

My SQL Query:

SELECT Save_Burger.Name, Rating.TotalScore, Rating.TotalVotes FROM Save_Burger INNER JOIN Rating ON Save_Burger.RatingID=Rating.RatingID ORDER BY Rating.TotalScore DESC

Which returns:

|---------------------------------------|
|  Name   |  TotalScore  |  TotalVotes  |
|Burger1  |     11       |      3       |
|Burger2  |      6       |      2       |
|Burger3  |      5       |      5       |
|Burger4  |      2       |      1       |
|Burger5  |      0       |      0       |
|---------------------------------------|

I then use PHP to divide the TotalScore and TotalVotes:

if (($row["TotalScore"] != 0) || ($row["TotalVotes"] != 0)) {
            $TotalRatingScore = $row["TotalScore"] / $row["TotalVotes"];
            echo substr($TotalRatingScore, 0, 3);
        }
        else {
            echo 0;

The code is working fine. The problem is it doesnt echo the highest divided value first. So even though Burger3 have an average rating of 1, its still topping Burger4 with an average rating of 2.

What would be the best choice? Should I divide from the SQL Query or is there a PHP fix? If so, how?

Thanks in advance,

Thomas

Was it helpful?

Solution

You need to order by the ratio:

ORDER BY Rating.TotalScore/Rating.TotalVotes DESC
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top