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

?>

 

 

Link to comment
Share on other sites

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  ::)

Link to comment
Share on other sites

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.