总结在html使用PHP

时间:2013-01-13 01:25:53

标签: php html mysql

为什么我不能总结收入这个代码只显示TOTAL的最后一笔收入:如何正确汇总?

<table border="1">
    <tr>
       <th>Date</th>
      <th>Route</th>
      <th>Destination</th>
      <th>Van No.</th>
      <th>Waybill No.</th>
      <th>Charge Invoice</th>
      <th>Revenue</th>
      <th>Strip/Stuff</th>


    </tr>

  <?php do { ?>
    <tr>

      <td><?php echo $row_PK['delivery_details_date']; ?></td>
      <td><?php echo $row_PK['delivery_details_route']; ?></td>
      <td><?php echo $row_PK['delivery_details_destination']; ?></td>
      <td><?php echo $row_PK['delivery_details_van_no']; ?></td>
      <td><?php echo $row_PK['delivery_details_waybill_no']; ?></td>
      <td><?php echo $row_PK['delivery_details_charge_invoice']; ?></td>
      <td><?php echo $row_PK['delivery_details_revenue']; ?></td>
      <td><?php echo $row_PK['delivery_details_strip_stuff']; ?></td>

     <?php $revenue = $row_PK['delivery_details_revenue'];
      $sum += $revenue;?>

        </tr>  
          <?php } while ($row_PK = mysql_fetch_assoc($PK));?>





      </table>

TOTAL: <?php echo $revenue; ?> <br/>

TOTAL只是显示最后记录的收入,它没有添加所有这些为什么?

1 个答案:

答案 0 :(得分:0)

首先请注意一个错误,我注意到你在结尾处回显你的$ revenue变量,这只是最后加载的数据...尝试回显$ sum,如果这是一些意想不到的结果,我们可以查看for的结构环。但是,变量交换可能是唯一的问题。

同样对于你的while循环的结构我觉得把你的条件置于顶部是更安全的...这可能有用但是每当我看一个while循环时条件是在顶部所以最好尝试并遵循共同的惯例。 e.g。

<?php
while ($row_PK = mysql_fetch_assoc($PK)) {
?>
//your code
<?php
 $revenue = $row_PK['delivery_details_revenue'];
      $sum += $revenue;
}
echo $sum;
?>