我想在每一行的末尾显示每一行的总价值...所有记录都正在从数据库中获取,但是如何计算每一行的总价值以及如何在每一行的末尾显示呢?请帮忙。



<table class="rwd-table ui form" id="myTable">


 <thead>
  <tr>
    <th width="50%">DATE</th>
    <th>TARGET DAILY CONSUMPTION MMBTU</th>
    <th>CONSUMPTION FROM 00H TO 12H IN MMSCF</th>
    <th>CONSUMPTION FROM 12H TO 24H IN MMSCF</th>
    <th>CONSUMPTION PER RAK GAS IN MMBTU</th>
    <th>REASON OF VARIATION OF CONSUMPTION BETWEEN ACTUAL & TARGET</th>
  </tr>
 </thead>
 <tbody>
    <?php foreach ($view as $report) {?>
    <tr style="color: #000000;">
    <td><?php echo $report['date'];?></td>
    <td><?php echo $report['Target Daily Consumption in MMBTU'];?></td>
    <td><?php echo $report['Consumption From 00h to 12h in MMSCF'];?></td>
    <td><?php echo $report['Consumption From 12h to 24h in MMSCF'];?></td>
    <td><?php echo $report['Consumption As Per RAK Gas in MMBTU'];?></td>
    <td><?php echo $report['Reason of Variation of Consumption Between ACTUAL & TARGET'];?></td>
  </tr>
    <?php }?>
    </tbody>
</table>

最佳答案

您可以简单地在循环之前添加一个变量,例如:

$tot = 0;


然后在sum_total calc之后添加:

$tot += $sum_total;


我还将对sum_total进行一些更改(如果您使用整数):

$sum_total = intval( $row['stock_count'] ) * intval( $row['stock_price'] );


或(如果使用浮点数):

$sum_total = floatval( $row['stock_count'] ) * floatval( $row['stock_price'] );


与:

echo number_format( $sum_total, 2 );


您可以使用2位小数来打印浮点数。

09-25 16:44