tags:

views:

37

answers:

1

I need to know how to make a rating script for a site. I have a form that submits a rating out of ten to mysql. How would you get the average rating to be displayed from the mysqk column?

---using PHP with mysql

+3  A: 

the best way is to store 2 columns - rate_count and rate_total. then when you get a new rating you just issue an update like this :

update rating_table set rate_count = rate_count + 1, rate_total = rate_total + $rating where id = $id

then to get the average you retrieve rate_count and rate_total. as long as rate_count is >0, rate_total/rate_count will give you your average rating.

oedo