用html总结html [关闭]

Why can't i sum the revenue this code just displays the last revenue in TOTAL: how to properly sum?

<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 just shows the last recorded revenue it does not add all of them why?

On first glace one error i notice is that you echo your $revenue variable at the end which is just the last loaded data... try echoing $sum and if that is some unexpected result we can look at the structure of for loop. But that variable swap may be the only issue.

Also for the structure of your while loop I feel like it is safer to put your conditional on the top... this may work but whenever I look at a while loop the conditional is at the top so it is good to try and follow common convention. e.g.

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