C
Craig M
Guest
There has to be a shorter way of finding the average rating for a clip/story/picture that has been rated multiple times than what i have below, its basically a youtube style rating system (5 star), and i have managed to find the average rating for clips, but i know theres an easier, quicker and more effeccient way of typing it, any help?:
<?php
$one = mysql_num_rows(mysql_query("SELECT * FROM
`clip_ratings` WHERE `clip_id`='1' and `rating`='1'"));
$two = mysql_num_rows(mysql_query("SELECT * FROM
`clip_ratings` WHERE `clip_id`='1' and `rating`='2'"));
$three = mysql_num_rows(mysql_query("SELECT * FROM
`clip_ratings` WHERE `clip_id`='1' and `rating`='3'"));
$four = mysql_num_rows(mysql_query("SELECT * FROM
`clip_ratings` WHERE `clip_id`='1' and `rating`='4'"));
$five = mysql_num_rows(mysql_query("SELECT * FROM
`clip_ratings` WHERE `clip_id`='1' and `rating`='5'"));
$total = ($one+($two*2)+($three*3)+($four*4)+($five*5));
$rating = ($total/($one+$two+$three+$four+$five));
echo "Total: $total<br><br>";
echo "Rating: $rating";
?>
Thanks Dan!
From the above script to:
<?php
$total = mysql_fetch_array(mysql_query("SELECT
SUM(rating) AS sum_rating FROM
`clip_ratings` WHERE `clip_id` = '1'"));
$count =
mysql_num_rows(mysql_query("SELECT *
FROM `clip_ratings` WHERE `clip_id` =
'1'"));
$rating = ($total[sum_rating]/$count);
echo "Rating: $rating";
?>
<?php
$one = mysql_num_rows(mysql_query("SELECT * FROM
`clip_ratings` WHERE `clip_id`='1' and `rating`='1'"));
$two = mysql_num_rows(mysql_query("SELECT * FROM
`clip_ratings` WHERE `clip_id`='1' and `rating`='2'"));
$three = mysql_num_rows(mysql_query("SELECT * FROM
`clip_ratings` WHERE `clip_id`='1' and `rating`='3'"));
$four = mysql_num_rows(mysql_query("SELECT * FROM
`clip_ratings` WHERE `clip_id`='1' and `rating`='4'"));
$five = mysql_num_rows(mysql_query("SELECT * FROM
`clip_ratings` WHERE `clip_id`='1' and `rating`='5'"));
$total = ($one+($two*2)+($three*3)+($four*4)+($five*5));
$rating = ($total/($one+$two+$three+$four+$five));
echo "Total: $total<br><br>";
echo "Rating: $rating";
?>
Thanks Dan!

<?php
$total = mysql_fetch_array(mysql_query("SELECT
SUM(rating) AS sum_rating FROM
`clip_ratings` WHERE `clip_id` = '1'"));
$count =
mysql_num_rows(mysql_query("SELECT *
FROM `clip_ratings` WHERE `clip_id` =
'1'"));
$rating = ($total[sum_rating]/$count);
echo "Rating: $rating";
?>