5 star rating system messes up my numbers

Thread Solved

Join Date: Oct 2006
Posts: 198
Reputation: MaxMumford is an unknown quantity at this point 
Solved Threads: 1
MaxMumford's Avatar
MaxMumford MaxMumford is offline Offline
Junior Poster

5 star rating system messes up my numbers

 
0
  #1
Oct 6th, 2008
Hi,

Im making a 5 star rating system but half the time it has the complete wrong result. I have gone over and over the script and checked the sums with a calculator and it is still doing something wierd. Sometimes when I rate it above it's current rating it moves the overall rating down...

The page that links to the php file just has links to the php file with the appropriate PID (product ID) and user's rating, for example index.php?pid=1234&rating=5. The ratings are between 1-5.

anyway, the code I currently have is below:

(by the way, apologies for the crappy notes, im not a mathmatician so find it difficult to explain what i'm doing.)

  1. //get product id and user's rating
  2. $pid = $_GET['pid'];
  3. $newrating = $_GET['rating'];
  4.  
  5. //connect to db
  6. include('../../php/database/connect.php');
  7.  
  8. //get current values from that product in the db
  9. $query = "SELECT * FROM products WHERE pid = '".$pid."'";
  10. $result = mysql_query($query);
  11. while($row = mysql_fetch_array($result, MYSQL_ASSOC))
  12. {
  13. //get current number of rates
  14. $numberofrates = $row['numberofrates'];
  15. $numberofrates = (int)$numberofrates;
  16. //get current rating
  17. $rating = $row['rating'];
  18. $rating = (int)$rating;
  19. //multiply current rating by number of ratings then increment number of rates by 1 to include new rating, and add user's rating to total rates to recreate average
  20. $rating = $rating * $numberofrates;
  21. $newnewrating = $rating + $newrating;
  22. $numberofrates = $numberofrates + 1;
  23. $newnewrating = $newnewrating / $numberofrates;
  24.  
  25. mysql_query('UPDATE `web163-zavvex`.`products` SET `rating` = "'.$newnewrating.'" WHERE `products`.`pid` = "'.$pid.'" LIMIT 1');
  26. mysql_query('UPDATE `web163-zavvex`.`products` SET `numberofrates` = "'.$numberofrates.'" WHERE `products`.`pid` = "'.$pid.'" LIMIT 1');
  27.  
  28. echo $newnewrating;
  29. }

Thanks in advance for pointing out where I have gone wrong

Max
Last edited by MaxMumford; Oct 6th, 2008 at 2:59 pm.
Ill solve somebody's thread someday! xD
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: 5 star rating system messes up my numbers

 
0
  #2
Oct 6th, 2008
Originally Posted by MaxMumford View Post
  1. mysql_query('UPDATE `web163-zavvex`.`products` SET `rating` = "'.$newnewrating.'" WHERE `products`.`pid` = "'.$pid.'" LIMIT 1');
  2. mysql_query('UPDATE `web163-zavvex`.`products` SET `numberofrates` = "'.$numberofrates.'" WHERE `products`.`pid` = "'.$pid.'" LIMIT 1');
Why do you have to have "limit 1" on your update queries, is "pid" not your primary key. If not, maybe you are having issues with duplicate rows? This could produce unpredictable results.
Last edited by R0bb0b; Oct 6th, 2008 at 3:50 pm.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss

-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 198
Reputation: MaxMumford is an unknown quantity at this point 
Solved Threads: 1
MaxMumford's Avatar
MaxMumford MaxMumford is offline Offline
Junior Poster

Re: 5 star rating system messes up my numbers

 
0
  #3
Oct 6th, 2008
i got the mysql_query code from phpmyadmin and just left it as is. pid is my primary key so i can take it out..

iv just tried taking out the limit and it still has a problem. Here is a link to a preview page..

http://www.grafax.co.uk/OTHER/zavvex...p?category=all

just vote on any of them a few times and it will start messing up.
Ill solve somebody's thread someday! xD
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: 5 star rating system messes up my numbers

 
0
  #4
Oct 6th, 2008
I see a button to add to basket but I don't see a button to vote
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss

-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 198
Reputation: MaxMumford is an unknown quantity at this point 
Solved Threads: 1
MaxMumford's Avatar
MaxMumford MaxMumford is offline Offline
Junior Poster

Re: 5 star rating system messes up my numbers

 
0
  #5
Oct 6th, 2008
click one of the 5 stars
Ill solve somebody's thread someday! xD
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: 5 star rating system messes up my numbers

 
0
  #6
Oct 6th, 2008
I see it now, wouldn't work if firefox, switched to IE and I can see where the images are suppose to be. Only the rollovers work. Attached is what I see. Could have something to do with the network I am behind as well, maybe.
Click image for larger version

Name:	Untitled-1.jpg
Views:	4
Size:	196.3 KB
ID:	7625
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss

-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 198
Reputation: MaxMumford is an unknown quantity at this point 
Solved Threads: 1
MaxMumford's Avatar
MaxMumford MaxMumford is offline Offline
Junior Poster

Re: 5 star rating system messes up my numbers

 
0
  #7
Oct 6th, 2008
okay all the images and rollover's are sorted out now. it was just a simple mistake with the php script. try rating an item as 5 star about 5 times and eventually you will see the new rating on the next page decrease instead of decrease
Ill solve somebody's thread someday! xD
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: 5 star rating system messes up my numbers

 
0
  #8
Oct 6th, 2008
Your calculations are not correct if you are looking for an average, I have just gotten busy and will have to get back to you on the error shortly.
Last edited by R0bb0b; Oct 6th, 2008 at 5:11 pm.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss

-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 198
Reputation: MaxMumford is an unknown quantity at this point 
Solved Threads: 1
MaxMumford's Avatar
MaxMumford MaxMumford is offline Offline
Junior Poster

Re: 5 star rating system messes up my numbers

 
0
  #9
Oct 6th, 2008
okay, thanks a lot. reply when you have time
Ill solve somebody's thread someday! xD
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: 5 star rating system messes up my numbers

 
0
  #10
Oct 6th, 2008
OK, so what you are doing in your code is taking the current rating, adding the new rating to it, averaging that by dividing that by all votes. This will not work because you are not preserving the votes. For instance, if someone votes 5, what you do is take the 5 and add the current rating to it, but since lets say this is the first vote, the current rating is 0. So your current rating is 5 and the number of votes is 1 so you take 5/1 and that makes the current rating 5. Now take the second vote and follow the same process. Someone else comes around and votes 3. What you do is take the current rating add three to it and divide it by the number of votes so 8/2 makes the current rating 4, BUT rather than preserving that vote as 3 and using it in the next calculation when the 3rd person votes you store the average in rank. Is this making sense yet.

You can store rank at the time the vote was made but you HAVE to store the vote and calculate (vote+newvote)/(numvotes+1) what you are doing is (currentrank+newvote)/(numvotes+1). You must add up all the votes and divide by the number of votes not add up all the rankings at the time the votes were made + the new vote and divide by the number of votes.

Does this help?
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss

-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC