Thanks for that Shawn,
That almost works perfectly. The only thing is, it is missing the first result in the while loop.
eg. I have 9 records in the table with values of 2, 4, 1, 2, 5, 3, 5, 4, 3. Totaling 29.
My array output is this.....
Array ( [0] => 4 [1] => 1 [2] => 2 [3] => 5 [4] => 3 [5] => 5 [6] => 4 [7] => 3 )
As you can see it is missing off the first record value of 2.
Here is my code......
Code: Select all
$result = mysql_query("SELECT COUNT(*) FROM ratings WHERE imageID = '$imageID'");
$row = mysql_fetch_row($result);
$numrows = $row[0];
$query = "SELECT * FROM ratings WHERE imageID = '$imageID' ORDER BY ratingID DESC";
$result = mysql_query($query);
$rating = mysql_fetch_array($result);
while($rating = mysql_fetch_array($result)){
$ratings[] = $rating['rating'];
}
$sum_total = array_sum($ratings);
$new_rating = $sum_total/$numrows;
echo $new_rating;
echo $numrows;
echo $sum_total;
print_r($ratings);
Why would it be doing this. Thanks.
Paul