Jump to content

[SOLVED] Averaging the rating


J4B

Recommended Posts

So I'm looking for the best way to average in a rating someone gives a movie. For example:

 

The Matrix

Person 1 - Rates it 5

Person 2 - Rates it 3

Person 3 - Rates it 5

Person 4 - Rates it 4

Person 5 - Rates it 4

 

So the average is 4.2 but they dont all submit their rating at the same time for me to just average. So would I just have 2 columns in my movies database and have it as follows:

rating(A)-------num of ratings(B)

so then like follows:

(A*B+5)/(B+1)---(0*0+5)/(0+1)

(A*B+3)/(B+1)---(5*1+3)/(1+1)

(A*B+5)/(B+1)---(4*2+5)/(2+1)

(A*B+4)/(B+1)---(4.33*3+4)/(3+1)

(A*B+4)/(B+1)---(4.25*4+4)/(4+1)

4.2

 

But how would I write that properly?

 

Link to comment
https://forums.phpfreaks.com/topic/150195-solved-averaging-the-rating/
Share on other sites

can't you just do a total score and a number of scores. then

rating = total score / number of scores

so for each score, it would be:

$item = 123;//Id of the item rating
$score = 4;//This is the score that was just submitted
UPDATE ratings SET total_score = total_score + $score, num_scores = num_score + 1 WHERE item_id = $item

 

and then to getting the score:

SELECT total_score/num_score as rating WHERE item_id = $item

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.