我从数据库中检索了一些值。在我的视图文件中,我有以下内容:
$row['fee_amount'];
现在,我想总结一下$ row ['fee_amount']中的所有值;然后展示它。
我知道在查询数据库时我可以总结,但我有兴趣学习如何使用PHP添加。
请你教我怎么做?
<?php if(count($records) > 0) { ?>
<table id="table1" class="gtable sortable">
<thead>
<tr>
<th>S.N</th>
<th>Fee Type</th>
<th>Fee Amount</th>
</tr>
</thead>
<tbody>
<?php $i = 0; foreach ($records as $row){ $i++; ?>
<tr>
<td><?php echo $i; ?>.</td>
<td><?php echo $row['fee_type'];?></td>
<td><?php echo $row['fee_amount'];?></td>
</tr>
<?php } ?>
</tbody>
<tr>
<td></td>
<td>Total</td>
<td>
I WANT TO DISPLAY THE SUMMATION RESULT HERE ADDING UP VALUES INSIDE THIS>>> <? $row['fee_amount']; ?>
</td>
</tr>
</table>
<?php } ?>
答案 0 :(得分:3)
在您的视图文件中,使用foreach
循环,在$sum
计数器旁边添加$i
变量并添加每次迭代的数量(类似于增加{{1} }}):
$i
(我把它放在多行上以使其更具可读性。)
<?php
$i = 0;
$sum = 0;
foreach ($records as $row)
{
$i++;
$sum += $row['fee_amount']; ?>
完成后,foreach
包含总金额:
$sum
这很简单。您只需要一个新变量( <td>Total: <?php echo $sum; ?></td>
)并进行计算。
答案 1 :(得分:2)
使用循环
$sum = 0;
while($row...){
$sum += $row['fee_amount']
}
echo $sum;
答案 2 :(得分:0)
你可以使用;
$someValue = 0; foreach($row["fee_amount"] as $value) { $someValue = $someValue + $value; }
答案 3 :(得分:0)
使用这个php函数,如果$ row ['fee_amount']是一个数组^ _ ^
例如:
$a = array(2, 4, 6, 8);
array_sum($a)