Jump to content


Photo

Reversing a rating


  • Please log in to reply
6 replies to this topic

#1 goatboy

goatboy
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 20 September 2006 - 06:15 PM

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

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


#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 20 September 2006 - 06:18 PM

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.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 goatboy

goatboy
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 20 September 2006 - 06:34 PM

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

#4 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 20 September 2006 - 06:39 PM

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.

#5 goatboy

goatboy
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 20 September 2006 - 06:45 PM

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?

while(list($rating) = mysql_fetch.....{

 what goes here?

}



#6 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 20 September 2006 - 07:14 PM

SELECT SUM(ratings) AS sum, AVG(ratings) AS avg, COUNT(ratings) AS count FROM ratings table;

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.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#7 goatboy

goatboy
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 20 September 2006 - 08:49 PM

sweet, thats perfect.

Thanks a lot!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users