我已经从数据库中检索了一些值。在我的视图文件中,我有以下内容:
$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 } ?>发布于 2011-11-27 23:12:54
在视图文件中,使用foreach循环,在$i计数器旁边添加一个$sum变量,并添加每次迭代的数量(类似于增加$i):
<?php
$i = 0;
$sum = 0;
foreach ($records as $row)
{
$i++;
$sum += $row['fee_amount']; ?>(我将其放在多行中,以使其更具可读性)。
foreach完成后,$sum包含总金额:
<td>Total: <?php echo $sum; ?></td>就是这么简单。您只需要一个新变量($sum)并进行计算。
发布于 2011-11-27 20:18:25
使用循环
$sum = 0;
while($row...){
$sum += $row['fee_amount']
}
echo $sum;发布于 2011-11-27 20:33:56
使用此php函数时,如果$ ^_^‘’fee_amount‘是数组行
例如:
$a = array(2, 4, 6, 8);
array_sum($a)https://stackoverflow.com/questions/8285475
复制相似问题