contra10 Posted January 29, 2009 Share Posted January 29, 2009 i'm using this to get the average of a rating but it only calculates the lates input divided by the number of rows... <?php $query="SELECT * FROM `ratings` WHERE `ratedid`= '$id'"; $nt=mysql_query($query); echo mysql_error(); $countofrate= mysql_num_rows($nt); while($row=mysql_fetch_array($nt)){ $rateresults = "{$row['rate']}"; } $average = $rateresults / $countofrate; print("Rated: $average"); ?> i don't know if COUNT is supposed to be used or if there is a way to add up all the rows... Quote Link to comment Share on other sites More sharing options...
flyhoney Posted January 29, 2009 Share Posted January 29, 2009 SELECT AVG(rate) FROM ratings Quote Link to comment Share on other sites More sharing options...
milesap Posted January 29, 2009 Share Posted January 29, 2009 <?php $query="SELECT * FROM `ratings` WHERE `ratedid`= '$id'"; $nt=mysql_query($query); echo mysql_error(); $countofrate= mysql_num_rows($nt); $rateresults = 0; while($row=mysql_fetch_array($nt)){ $rateresults = $rateresults + $row['rate']; } $average = $rateresults / $countofrate; print("Rated: $average"); ?> Quote Link to comment Share on other sites More sharing options...
printf Posted January 29, 2009 Share Posted January 29, 2009 Something like... $query = "SELECT (SUM(rate)/COUNT(*)) AS average FROM ratings WHERE ratedid = '" . $id . "';"; Quote Link to comment Share on other sites More sharing options...
contra10 Posted January 29, 2009 Author Share Posted January 29, 2009 thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.