This should be easy but for some reason it’s escaping me. I have a very simple query and loop here. I didn’t add the actual query…but the array of objects in $results. The $totalHits addition works fine. Totaling up the number in $iratings works fine as well. What I am also trying to do is get the count of records in $iratings and print out an average…I just can’t seem to get the count part working. How can I modify this to grab the count of $iratings then divide the TotalRating by the count of the number of “$iratings” in the array? if that makes sense?
$result = $db->loadObjectList();
// Tally Up the Stats
$totalHits = 0;
$totalRating = 0;
foreach ($result as $item) {
$ihits = $item->ihits;
$totalHits = $totalHits + $ihits;
$iratings = $item->iratings;
$totalRating = $totalRating + $iratings;
}
echo ('<br/>Total Hits: '.$totalHits);
echo ('<br/>Total Rating: '.$totalRating);
1 Answer