Link to home
Start Free TrialLog in
Avatar of pnoeric
pnoericFlag for United States of America

asked on

best algorithm to keep track of star ratings

Hi, I'm trying to figure out the best way to keep track of star ratings (1 star, 2 stars etc up to 5 starts) in my code. Users look at content, give it a rating, and I need to keep track of the average and report it back. (Just like you see on YouTube, below the video, for example.)

Right now my plan is:

1. Users submits rating (1-5)

2. I add it to running total of ALL ratings ever (so this will be a very big integer)

3. I increase the number of people who have rated by one

4. I divide total by # of people and that gives me the average


So I'm keeping two database fields (running_total and how_many_ratings) and doing some math to arrive at the solution. It works, though I'm worried about running_total getting too big at some point.

Is there a more elegant way to handle this in my app? The goal is to keep the least amt of info in the database, etc.
i.e.:
 
   $running_total += $current_rating
 
   $how_many_ratings++
 
   $average_rating = $running_total / $how_many_ratings

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ee_rlee
ee_rlee
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial