Page 1 of 1

Poll average/rating?

Posted: Tue Feb 25, 2003 1:27 am
by Mr. Tech
I have serached but there where to many results and none of the posts that I looked at really helped me.

What I need to do is get the average of a Poll(This should be simple I hope :roll: )

What I have is in the MySQL Database I have 10 table rows:

v1
v2
v3
v4
v5
v6
v7
v8
v9
v10


When someone votes(e.g 6) it will update the row(in this case v6) that is the same number as voted.

Now how do I get the average rating for each link? What I have tried is this:

// Counts how many votes
$votes = $v1 + $v2 + $v3 + $v4 + $v5 + $v6 + $v7 + $v8 + $v9 + $v10;

// Calculates the rating... Don't know what to put for the ?????
$temp_rating = ($votes) ? ????? / $votes : 0 ;
$rating=number_format($temp_rating , 2);

echo "Rating: $rating";
I got this code from another script. What I'm not sure about is what I put where the ?????'s are...

Does anyone have some code or can fix up my code?

Thanks for any help!

If I didn't make sense please tell me.

Thanks

Posted: Tue Feb 25, 2003 2:04 am
by jiop
i think it should be

їcode]
// Counts how many votes
$votes = $v1 + $v2 + $v3 + $v4 + $v5 + $v6 + $v7 + $v8 + $v9 + $v10;

// Calculates the rating... Don't know what to put for the ?????
$temp_rating = ($votes) ? $votes / $total_votes : 0 ;
$rating=number_format($temp_rating , 2);

echo "Rating: $rating";
ї/code]

where $total_votes is max possible number of rating possible. for example if someone voted 1 and 2, then $total_votes should be out of 20 since the max possible it 20.

unless i totally missed the point of what you were trying to do.

btw what is this for? picture rating site?

Posted: Tue Feb 25, 2003 5:20 am
by volka
you probably want the arithmetic mean, which is the sum of votes divided by count of votes.

Code: Select all

$sum = $v1 + 2*$v2 + ... + 10*$v10;
$temp_rating = ($votes) ? ($sum / $votes) : 0 ;
e.g.
3 votes for "5" and 7 votes for "6":
$sum = 15+42 = 57
$temp_rating = (57 / 10) = 5.7

Posted: Tue Feb 25, 2003 6:17 pm
by Mr. Tech
volka wrote:you probably want the arithmetic mean, which is the sum of votes divided by count of votes.

Code: Select all

$sum = $v1 + 2*$v2 + ... + 10*$v10;
$temp_rating = ($votes) ? ($sum / $votes) : 0 ;
e.g.
3 votes for "5" and 7 votes for "6":
$sum = 15+42 = 57
$temp_rating = (57 / 10) = 5.7
So $sum should be:

$sum = $v1 + 2*$v2 + 3*$v3 + 4*$v4 + 5*$v5 + 6*$v6 + 7*$v7 + 8*$v8 + 9*$v9 + 10*$v10;

Thanks