Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/176378-solved-php-rating-system-using-mysql/
Share on other sites

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");

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.