动态添加到数组中并检索总和


Dynamically add into array and retrieve sum

foreach ($_POST['ECL'] as $lt) {
    //SQL select statements are run.  Each $lt is a where condition where results are obtained from the DB.
}

我想对每个结果进行计数并总结一下。 总数将是获得的记录。我可以获取每个$lt的记录计数,但我无法将它们相加。 所有的帮助将不胜感激。

[更新] $lt的数量不是固定的。 $lt可以是 1,2,3,4... 诸如 $i++ 之类的计数器不起作用

在循环之前设置一个计数器,在循环期间递增它,在最后检索总和。简单。。。

尝试使用 mysql_num_rows 或 mssql_num_rows - 它们返回结果中的行数

$count = 0;  // setup count variable
foreach ($_POST['ECL'] as $lt) {
    //SQL select statements are run.  Each $lt is a where condition where results are obtained from the DB.
    $count += mysql_num_rows($result);  // add results count to our counter
}
echo $count; // this will be the total number of rows the queries returned

您的问题说您要将每个$lt的结果行添加到数组中(不过我在发布的代码中没有看到)。

如果是这种情况,并且您每行插入一条数组记录,则数组的长度应作为您的计数。