Forum Moderators: coopster

Message Too Old, No Replies

What to do?

Little math problem

         

smatts9

9:16 pm on Mar 7, 2006 (gmt 0)

10+ Year Member



I have a database that is full of reviews for certain items, and when user's submit their review there was also an option to vote it out of 5. The table I have set up has the following columns:

-itemname
-date of review submission
-review text
-rank (number 1-5)
-reviewer's name
-unique id (auto increment)

All the reviews for evey item is in this table, I sort them out by itemname when displaying them. Now I would like to display a rank out of 5, the average. I messed up when making the table and should have made a column for total-rankings for each item, but now it is too late?... I was wondering if anyone has an idea on how to use the numbers? Is there ways of getting the total number of entries for a certain itemname so I can use that number to get an average? Thanks for your help.

[edited by: coopster at 1:06 am (utc) on Mar. 8, 2006]
[edit reason] Fixed the title to say 'math' [/edit]

lobo235

10:50 pm on Mar 7, 2006 (gmt 0)

10+ Year Member



This should not be that difficult. Just select all of the rows with the item name and use the mysql_num_rows() php function to see how many reviews there are for the item. Then add all the ranks together and divide by the number of reviews for the item. Just a standard averaging operation.

whoisgregg

11:05 pm on Mar 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can have MySQL do the heavy lifting here also. Although it would most likely be best to run this as a cron and update the items with the result.

SELECT COUNT(*) as totalrows,AVG(rank) as averagerank FROM `reviews_table` WHERE `itemname`='blue_fuzzy_widget';

Added: Untested pseudocode, btw.