jokerbla Posted October 3, 2009 Share Posted October 3, 2009 Hi, this isn't really a mysql matter but it's the most appropriate section to post in. I'm trying to make a rate system script in php and I'm using a database to store the voters and the average so far. So each time someone makes a new vote, average must be recalculated and voters is increased by one. I'm having trouble with the maths, I've been over this for like 4 hours, and I'm not sure what the problem is. newaverage = (average / voters + newvote) / (voters + 1) .. and then I update the database and print the new average, but it doesn't work right. What's wrong with it? Quote Link to comment https://forums.phpfreaks.com/topic/176378-solved-php-rating-system-using-mysql/ Share on other sites More sharing options...
jokerbla Posted October 3, 2009 Author Share Posted October 3, 2009 might it be something else I did wrong? $sgvote = mysql_query("SELECT * FROM vote") or die(mysql_error()); while($ratings = mysql_fetch_array( $sgvote )) { $voters = $ratings["voters"]; $average = $ratings["average"]; } $newaverage = ($average / $voters + $choice) / ($voters+1); $voters = $voters + 1; $update = mysql_query("UPDATE vote SET voters=$voters, average=$newaverage"); Quote Link to comment https://forums.phpfreaks.com/topic/176378-solved-php-rating-system-using-mysql/#findComment-929612 Share on other sites More sharing options...
cags Posted October 3, 2009 Share Posted October 3, 2009 I think you'd be looking at something like... $new_average = (($old_average * $old_vote_count) + $new_vote) / $old_vote_count + 1; Quote Link to comment https://forums.phpfreaks.com/topic/176378-solved-php-rating-system-using-mysql/#findComment-929642 Share on other sites More sharing options...
jokerbla Posted October 3, 2009 Author Share Posted October 3, 2009 lol I actually figured that out after some fresh air and when I saw your answer I was relieved, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/176378-solved-php-rating-system-using-mysql/#findComment-929730 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.