goatboy Posted September 20, 2006 Share Posted September 20, 2006 i've been succesfully using the 5 star rating system from phpfreaksbut 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] Quote Link to comment Share on other sites More sharing options...
obsidian Posted September 20, 2006 Share Posted September 20, 2006 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. Quote Link to comment Share on other sites More sharing options...
goatboy Posted September 20, 2006 Author Share Posted September 20, 2006 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 Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted September 20, 2006 Share Posted September 20, 2006 1. Delete the vote.2. Select everything from the table.3. Get the number of rows returned4. Set the total number of votes to what you got in number 3. Quote Link to comment Share on other sites More sharing options...
goatboy Posted September 20, 2006 Author Share Posted September 20, 2006 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] Quote Link to comment Share on other sites More sharing options...
obsidian Posted September 20, 2006 Share Posted September 20, 2006 [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. Quote Link to comment Share on other sites More sharing options...
goatboy Posted September 20, 2006 Author Share Posted September 20, 2006 sweet, thats perfect.Thanks a lot! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.