需要每行总值,并且应该显示在行尾

时间:2016-09-17 08:35:45

标签: javascript php html mysql sql

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



<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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您可以在循环之前添加变量,如:

$tot = 0;

然后在sum_total计算后添加:

$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位小数打印浮点数。