这应该很容易,但出于某种原因,它正在逃避我。我有一个非常简单的查询和循环。我没有添加实际查询...但是$ results中的对象数组。 $ totalHits添加工作正常。总计$ iratings的数量也很好。我还要做的是获取$ iratings中的记录数并打印出平均值......我似乎无法让计数部分正常工作。如何修改它以获取$ iratings的数量,然后将TotalRating除以数组中“$ iratings”的数量?如果这有意义吗?
$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);
答案 0 :(得分:0)
$result = $db->loadObjectList();
// Tally Up the Stats
$totalHits = 0;
$totalRating = 0;
$numIterations = 0;
foreach ($result as $item) {
$ihits = $item->ihits;
$totalHits = $totalHits + $ihits;
$iratings = $item->iratings;
$totalRating = $totalRating + $iratings;
$numIterations++;
}
echo ('<br/>Total Hits: '.$totalHits);
echo ('<br/>Total Rating: '.$totalRating);
echo ('<br />Average Rating: ' . ($totalRating/$numIterations));