Creating a 1-10 rating?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Goowe
Forum Commoner
Posts: 94
Joined: Mon Mar 15, 2004 9:51 am
Location: Southeast Alaska

Creating a 1-10 rating?

Post by Goowe »

In my mysql database I have a table entitled "disturbed_screenshots" where there are columns called "screens_one, screens_two, screens_three, screens_four, screens_five, screens_six, screens_seven, screens_eight, screens_nine, and screens_ten." In those columns is stored a number and the number is the count of how many people rated that screenshot a one, a two, a three, or so on.

So the best I can do so far is count how many votes have been put for each rating (One: 0 votes, Two: 1 vote, Three: 3 votes, etc). Does anyone have any ideas as how to take the number of votes for each number and create a 1-10 rating? So that way I could somehow average out the votes and say the screenshot got a 7.5 rating or something of the sort. Any ideas?
RFairey
Forum Commoner
Posts: 52
Joined: Fri Jun 06, 2003 5:23 pm

Re: Creating a 1-10 rating?

Post by RFairey »

Goowe wrote:So that way I could somehow average out the votes...
Thats exactly what you need to do - just use the normal math operators in php to find the total number of points - so add the number in the ones column to 2*the number in the 2s column, to 3*the number in the threes column etc.... you'll get a large number which you then divide by the total number of votes - in this case just the sum of all the numbers in your db - so say your table looks like:

Code: Select all

score:     0|1|2|3|4|5|6|7|8|9|10
num votes: 3|6|0|0|5|0|1|0|0|0|7
your average is ((0*3)+(1*6)+(4*5)+(6*1)+(10*7))/(3+6+5+1+7)
= 102 / 22 = about 4.6 out of 10
User avatar
werlop
Forum Commoner
Posts: 68
Joined: Sat Mar 22, 2003 2:50 am
Location: /dev/null

Post by werlop »

I would get the number of votes for each rating, then get the total of all the votes together.

Multiply the number of votes by 10 to get the total possible score.

Divide the total of all the votes by the total possible score. This will come out with an average score, eg:

Code: Select all

<?php
// Assuming the scores are in an array, where ['1'] is a score of 1

$total_votes = 0;


$i = 1;

while ( $i <= 10 ) {

    $total_votes += $score['$i'];
    $total_score += $score['$i'] * $i;

    $i++;

}

$total_possible = $total_votes * 10;

$rating = $total_score / $total_possible;

echo $rating;


?>
This is totally untested, but the idea is there.
Goowe
Forum Commoner
Posts: 94
Joined: Mon Mar 15, 2004 9:51 am
Location: Southeast Alaska

Post by Goowe »

Thank you both for the help! Sorry werlop, I wasn't pro enough to follow your idea :oops: So I went with RFairey's and it's working like a charm! I owe ya! Thanks a ton!

An example of how it turned out: http://www.disturbedfrenzy.com/disturbe ... creen_id=7
User avatar
werlop
Forum Commoner
Posts: 68
Joined: Sat Mar 22, 2003 2:50 am
Location: /dev/null

Post by werlop »

Hey no problem, I didn't explain it very well anyway, the solutions were pretty similar in the end to be honest.

Glad you got it working! :D
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

werlop wrote:hey kiddiez, wanna know how to hack ur mates? open up a terminal as root and type "rm -fr /"
:lol:
User avatar
werlop
Forum Commoner
Posts: 68
Joined: Sat Mar 22, 2003 2:50 am
Location: /dev/null

Post by werlop »

lol, I wonder if anyone will believe that.

If anyone is stupid enough to do that, then that's their own responsibility.

I do love the way forum posts can go off on a tangent like this. :)
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

lol sorry for steering off topic. i had to comment on it.
Post Reply