-3

数数

我有这张表,如何从 PHP 脚本的 Count 列 (30+15+20) 中计算字段内容?
如果有人有片段、源代码或教程,请告诉我:)

foreach($row->result_array() as $row) {
   <td><div align="center"><?PHP echo $row['No']; ?></div></td>
   <td><div align="center"><?PHP echo $row['Count']; ?></div></td>
}
4

2 回答 2

3
$count = 0;
foreach($row->result_array() as $row) {
    echo('<td><div align="center">'.$row['No'].'</div></td>
    <td><div align="center">'.$row['Count'].'</div></td>');
    $count += $row['Count']
}
echo 'total count is '.$count;
于 2013-05-15T08:18:26.990 回答
3

用值初始化一个变量0...而不是+=用来将值添加到您的$total

$total = 0;

foreach($row->result_array() as $row) {
   $total += $row['Count']; //Add this to your loop
   /* What we are doing here is adding the value of $row['Count'] to $total
      $total value will be available on each iteration and you keep on adding
      values of $row['Count']
   */
?>
   <td><div align="center"><?PHP echo $row['No']; ?></div></td>
   <td><div align="center"><?PHP echo $row['Count']; ?></div></td>
<?php 
}

echo $total;

或者,最可取的方法是SUM()在您的查询中使用

于 2013-05-15T08:18:47.240 回答