0

这应该很容易,但由于某种原因它正在逃避我。我在这里有一个非常简单的查询和循环。我没有添加实际的查询...但是 $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);
4

1 回答 1

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));
于 2012-08-01T19:42:20.187 回答