Hello,
I have changed the layout of the ratings to display only the numbers instead of the stars pictures. This works fine except on the display of the average rating (with one decimal). In this case the average number is rounded off. For example a rating of 3 and a rating of 4 becomes on average 3,0 (instead of 3,5).
How can i change this?
Thanks!
Hello Marco,
This is happening because the rating is stored in the database as an Integer; so it relies on the rounding performed by MySQL when inserting a FLOAT value into an INT field, which is to round down.
One option would be to change the rating to type FLOAT; and then display the value using sprintf() to make sure you are using the number of decimal places you require. For example:
<?php
print "Rating: ".sprintf("%.1f",$product["rating"]);
?>
Remember - this will only work after changing the column "rating" in the products table from INT to FLOAT. The easiest way to do this would be to use phpMyAdmin or similar MySQL admin tool to modify the table directly. This won't have an immediate impact, because the values will already have been rounded. To update the values, you will need to create and moderate in another review; as this is when the ratings are updated.
Alternatively, for a fresh install you could modify setup.sql. The rating column is specified by the following SQL on line 93:
rating int(11) NOT NULL default '0',
Simply change this to:
rating float(10,2) NOT NULL default '0.0',
Finally, this will mean that the code to display the stars image will no longer work because this relies on the rating value being an integer. If you want to keep using the stars in certain places you would need to change the code that uses the $product["rating"] value to:
intval($product["rating"])
Hope this helps!
Cheers,
David.