Posts: 2,536
Location: Western Maryland
|
James, what I would do is not to store off the value of every vote and then recompute the average every single time.
Create a table called ratings:
pictureId <-- primary key
total <-- the total of all votes cast (e.g., 7,7,10,1,6 = 31)
votesCast <-- exactly that -- in the example above, 5.
So as a new vote is cast, retrieve the record for the picture, increment votesCast by 1 and total by the value of the vote.
You may even want (for faster searching) to have a fourth column already computing the average. In fact, I would recommend that (let's call it averageVote) so that you could then perform your SQL pulls based on that value:
PHP Code:
$sql = "SELECT * FROM ratings ORDER BY averageVote DESC"
__________________
—Kyrnt
|