Jump to content

Reversing a rating


goatboy

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]
Link to comment
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.
Link to comment
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
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.