Jump to content

Archived

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

goatboy

Reversing a rating

Recommended Posts

i've been succesfully using the 5 star rating system from phpfreaks
but i have one problem.
From time to time i'll need to delete someones rating. If i do that the rate total still shows the total rating with their vote included.
Does anyone know what reverse math to use so their vote is undone?

here's the math that adds their rating

[code]$new_count = ($num_votes + 1);
$total = $rate_total * $num_votes;
$total2 = (($rate + $total) / ($new_count));
$new_rating3 = number_format($total2, 2, '.', '');[/code]

Share this post


Link to post
Share on other sites
if you are simply storing the total number of votes and their average, there is no way to reverse it unless you have a database backup you can restore. if you are storing each individual vote in addition to the totals, you can simply delete their vote and then recalculate the total.

Share this post


Link to post
Share on other sites
yes, i store the individual persons vote in addition to the total in a different table for the item they are rating.

I just wanted to see if there was a quicker way to do it.

I think i'll just do it the way you mentioned. I also though about doing all the math on the fly and just storing the total number of votes and total rating but i don't know if thats a good idea if i have 20 rating listed on a page, that and i'd have to recalculate and change the table structure for all the votes i currently have.

Thanks

Share this post


Link to post
Share on other sites
1. Delete the vote.
2. Select everything from the table.
3. Get the number of rows returned
4. Set the total number of votes to what you got in number 3.

Share this post


Link to post
Share on other sites
besides counting the number of votes i need to add all the user ratings as well, i know i can use mysql_num_rows to count how many votes but how do i add all the ratings in the loop?

[code]
while(list($rating) = mysql_fetch.....{

what goes here?

}
[/code]

Share this post


Link to post
Share on other sites
[code]
SELECT SUM(ratings) AS sum, AVG(ratings) AS avg, COUNT(ratings) AS count FROM ratings table;
[/code]

that query (modified as you need it, of course) will return all the values you need for a 5 star rating system. so, once you delete the errant vote, just pull those values and have at it.

Share this post


Link to post
Share on other sites

×

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.