Forum Moderators: coopster

Message Too Old, No Replies

looking for a better php/mysql review/rating system

         

Skier88

1:48 am on Sep 13, 2010 (gmt 0)

10+ Year Member



A key functionality of my website is the review/rating system - people can either rate an item, in which case I store the rating, date, and ip address (to prevent repeated submissions), or they can write a review, in which case I also store the review text and author. I have a working system, but there are a few things about it I think should be improved. I'm asking here because I suspect that the ability to come up with a more efficient system goes hand in hand with php/mysql knowledge that I do not have.

First off, here's my current solution. Each item's database entry includes two fields for the rating process, num_ratings and sum_ratings. In addition to this, there is a separate table of reviews, with a field specifying which item each review belongs to. Every time an item is rated or reviewed num_ratings is incremented by 1, the rating is added to sum_ratings, and a row is added to the reviews table.

There are a few things I like about it:
  • avg rating and the number of ratings are easily obtainable, and therefore can be used on pages that display this information for many items
  • every rating is stored, leaving open the possibilities for more advanced rating display or analysis and gathering more information for every rating


But here's what worries me:
  • The same information is stored in two places, introducing the possibility of getting out of sync, increasing processing time and memory usage, etc
  • Reviews are not efficiently accessible. In order to distinguish them from ratings, I need to test the comments field of the review entry. This means that I need to loop through all the reviews to count or display them.
  • One of the most accessed numbers, the average rating, requires a calculation, albeit a short one.


My goal is to make this viable for even very large databases. Does anybody have a better method? It there a standardized method? I don't want a plugin, but I would be glad to study a snippet of code.

Thanks for reading.

enigma1

6:49 pm on Sep 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The average rating can be calculated on the fly instead and you can have various options to present the average (eg: total, per product, per category etc).

Your reviews table should contain an identifier about the product or page the review was posted. So when you retrieve the data you use the identifiers to get the desired rows only.

If you are concerned for latency getting the sum,average,mean or other statistical calculations/values, you can use another table and pre-calculate the results periodically. So a table of statistics could be updated on demand by the administrator instead of having the scripts processing the statistics with every request.