| | |
5 star rating system messes up my numbers
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
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.)
Thanks in advance for pointing out where I have gone wrong
Max
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.)
PHP Syntax (Toggle Plain Text)
//get product id and user's rating $pid = $_GET['pid']; $newrating = $_GET['rating']; //connect to db include('../../php/database/connect.php'); //get current values from that product in the db $query = "SELECT * FROM products WHERE pid = '".$pid."'"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { //get current number of rates $numberofrates = $row['numberofrates']; $numberofrates = (int)$numberofrates; //get current rating $rating = $row['rating']; $rating = (int)$rating; //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 $rating = $rating * $numberofrates; $newnewrating = $rating + $newrating; $numberofrates = $numberofrates + 1; $newnewrating = $newnewrating / $numberofrates; mysql_query('UPDATE `web163-zavvex`.`products` SET `rating` = "'.$newnewrating.'" WHERE `products`.`pid` = "'.$pid.'" LIMIT 1'); mysql_query('UPDATE `web163-zavvex`.`products` SET `numberofrates` = "'.$numberofrates.'" WHERE `products`.`pid` = "'.$pid.'" LIMIT 1'); echo $newnewrating; }
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
•
•
•
•
php Syntax (Toggle Plain Text)
mysql_query('UPDATE `web163-zavvex`.`products` SET `rating` = "'.$newnewrating.'" WHERE `products`.`pid` = "'.$pid.'" LIMIT 1'); mysql_query('UPDATE `web163-zavvex`.`products` SET `numberofrates` = "'.$numberofrates.'" WHERE `products`.`pid` = "'.$pid.'" LIMIT 1');
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.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
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.
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
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.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
“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.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
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.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
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?
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.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
![]() |
Other Threads in the PHP Forum
- Previous Thread: Need help with PHP dynamics
- Next Thread: connecting database
| Thread Tools | Search this Thread |
apache api array beginner binary body broken cakephp checkbox class cms code computing cron curl database date date/time delete display dynamic echo email error file files filter folder form forms function functions gc_maxlifetime global google host href htaccess html image include insert ip javascript joomla limit link list login mail memmory memory menu mlm msqli_multi_query multiple mycodeisbad mysql navigation oop parameter parsing paypal pdf php problem query radio random recourse recursion regex remote script search seo server sessions sms snippet source space sql static syntax system table thesishelp tutorial update upload url validator variable video web webdesign wordpress xml youtube





