Poll average/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
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Poll average/rating?

Post 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
jiop
Forum Newbie
Posts: 11
Joined: Wed Dec 18, 2002 4:00 am
Location: newport beach

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Post 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
Post Reply