-3

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

<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 只显示最后记录的收入它没有添加所有这些为什么?

4

1 回答 1

0

乍一看,我注意到一个错误是您在末尾回显了您的 $revenue 变量,这只是最后加载的数据...尝试回显 $sum 如果这是一些意外的结果,我们可以查看 for 循环的结构。但变量交换可能是唯一的问题。

同样对于您的while循环的结构,我觉得将条件放在顶部更安全......这可能有效,但是每当我查看while循环时,条件位于顶部,因此最好尝试遵循常见的惯例。例如

<?php
while ($row_PK = mysql_fetch_assoc($PK)) {
?>
//your code
<?php
 $revenue = $row_PK['delivery_details_revenue'];
      $sum += $revenue;
}
echo $sum;
?>
于 2013-01-13T01:31:15.120 回答