Jump to content

[SOLVED] Help with a rating script


mediabob

Recommended Posts

Hi, I am trying to create a rating system for my site where users can rate items x out of 5. I am having a bit of trouble with doing the math though, I am trying to get an average vote from all the votes atm i am doing it this way:

 

Current Vote + New Vote / # of votes

 

That doesn't seem to be correct though because no matter what vote you choose the total rating only goes down, I can vote 5 20 times and the rating will keep going down until it hits 0

 

Can anybody help with a better equation to figure an average rating for how many people voted 1-5

 

Thanks!!

Link to comment
https://forums.phpfreaks.com/topic/57985-solved-help-with-a-rating-script/
Share on other sites

You can simply get the average rating right from your query.

 

I am assuming your database table is set up something like this:

 

TABLE ratings

----------------

rating <--The rating the user gave the item

userID <--The person who rated the item

itemID <--The item that was rated

 

Now you can do a query like this:

 

<?php

$itemID = 23; //<---This is the items ID they are rating

$query = mysql_query("SELECT AVG(rating) as avg_rate FROM ratings WHERE itemID='$itemID'");
$row = mysql_fetch_assoc($query);

$rating = $row['avg_rate'];

echo $rating;

?>

 

 

Thanks I didn't think about doing it that way, I was actually just storing  the current vote and the total number of votes in a table and calculating the average with every vote and updating that table every time with a +1 to the total votes and the new average.

 

But this is a much better way because I was using seesions and a cookie to prevent multiple votes, this way I can check the db to see if a user voted on a file :)

 

You helped me in more way that one  ::)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.