OK, thanks guys, I have this working now, for those interested there's a code snippet below. The first bit of code sees whether there are approved reviews for a given ProductId, and then if there are, the second bit of the code calculates an average via the SELECT AVG(ReviewRating) MySQL statement.
Rgds
Code:
//Find out if there are approved reviews for a product
//Find out how many rows are in the table
$sql5 = "SELECT COUNT(*) FROM Reviews WHERE ProductId LIKE $ProductId AND ReviewIsApproved=1";
$result5 = mysql_query($sql5, $connection) or trigger_error("SQL", E_USER_ERROR);
$r5 = mysql_fetch_row($result5);
$numrows5 = $r5[0];
//If there are approved reviews, calculate the average review rating
If ($numrows5 > 0) {
//Calculate AverageReviewRating
$sql4 = "SELECT AVG(ReviewRating) FROM Reviews WHERE ProductId LIKE $ProductId AND ReviewIsApproved LIKE 1";
$result4 = mysql_query($sql4);
//obtain
while($row4 = mysql_fetch_array($result4)){
//mysql_num_rows($result4) always equal to 1 when calculating an average
//echo mysql_num_rows($result4);
$AverageReviewRating = $row4['AVG(ReviewRating)'];
//format to 2 decimal places
$AverageReviewRating = number_format($AverageReviewRating, 2);
echo " Average Rating: " . $AverageReviewRating . " / 10";
}
//End if there are approved reviews
}
Bookmarks