1. Home
  2. Computing & Technology
  3. PHP / MySQL
Angela Bradley
Angela's PHP / MySQL Blog

By Angela Bradley, About.com Guide to PHP / MySQL

5 Star Rating

Thursday May 25, 2006
Yesterday Chesso19 posted in our web forum:

"I've been trying to make a basic rating system that allows a user to click on one of five images, each representing a number from 1 to 5 (1,2,3,4 or 5) and somehow calculate a 1-5 rating from the results."

Our suggestion was that the most simple way to do this is to have one table column hold the total number of votes, and a second column hold the running total. Each time a vote is cast, we would add 1 to the total votes and add 1-5 (whatever they ranked it) to the running total. Then we could easily calculate the average rating by dividing the running total by the total votes. This 5 star rating tutorial shows an example of how it is done.

Obviously there are other ways (some more difficult than others) to do this. How would you go about creating a ranking system?
Comments
May 26, 2006 at 11:27 pm
(1) Chesso19 says:

Well the next day I kind of figured out my own way as I said in the forums (I hate when that happens, you need help and ask and then end up figuring it out….).

Anyway, it is a bit different and I quite like it, what I did was for the rating part, just simple have one Decimal type field in a table (I used the actual file entry’s table) with Length/Value as 1,2.

I had 5 images for rating 1-5 and they worked as follows:

Rating of 5 – Put rating field up by 0.10.
Rating of 4 – Put rating field up by 0.05.
Rating of 3 – Put rating field up by 0.03.
Rating of 2 – Put rating field down by 0.05.
Rating of 1 – Put rating field down by 0.05.

After updating the field I then check to make sure that rating is not below 0 and not above 5, otherwise they are reset to 0 or 5 respectively.

Then for display I simple get the value from the rating field and use the php function round() when displaying it to remove the decimal place values from it.

This seems to work fairly well, and a simple change of the 1-5 rating values will affect how easily the rating changes.

May 26, 2006 at 11:32 pm
(2) Chesso19 says:

Opps slight type there, for a rating of one it should go down by 0.10 (-0.10).

And just to clarify, when creating the decimal type field in the Length/Value box I put 1,2. Meaning 1 character long before the decimal place and 2 characters long after the decimal place (To allow up/down by two decimal places, e.g. 0.25).

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL

©2009 About.com, a part of The New York Times Company.

All rights reserved.