-1

我有一个简单的 PHP 脚本,它从 Joomla 数据库表中加载项目列表。

$result = $db->loadObjectList();

在此之后,我试图计算此列表中的项目总数:

foreach($result as $key=>$value){
$items = $value->item;
$count = count($items);
echo $count;
}

for each 循环输出111而不是添加这些值并提供3. 我知道这必须是一个简单的解决方法,我在循环中提取值的方式,但我似乎无法建立联系。任何想法都非常感谢。

4

3 回答 3

0
$count = 0;
foreach($result as $key=>$value){
    $items = $value->item;
    $count += count($items);
}
echo $count;
于 2013-10-26T21:32:09.913 回答
0

我认为 $items 是一个数组。如果我是对的,你可以做 smt。如下所示:

$result = $db->loadObjectList();
$items = array();

foreach($result as $key=>$value){
    $items[] = $value->item;
    $count = count($items);
    echo $count; //why echo here and why not outside the loop?
}
于 2013-10-26T21:41:03.867 回答
0

您可以在以下行的帮助下计算项目总数:

$result = $db->loadObjectList(); //your existing code line
echo count($result); //which gives you the total numbers
于 2013-10-27T07:00:38.840 回答