2013-01-13 25 views
-3

爲什麼不能我總結的收入中的代碼只是顯示在總營收最後:如何正確總結?總結在HTML中使用PHP

<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

你不是明明輸出的總和。 '$ sum + = $收入;'你的代碼?你知道它做了什麼嗎?然後再看看最後一個'echo'。 – mario

+0

除非您的$ row_PK變量設置在循環之前,否則您不能通過'do ... while'循環遍歷結果集,而這看起來不像循環。 – nickb

回答

0

在第一眼一個錯誤我注意到的是,你在這剛剛過去的加載數據的結束呼應你的$收入變量...嘗試呼應$和,如果是一些意想不到的結果,我們可以看一下結構for循環。但是這個變量交換可能是唯一的問題。

還爲您while循環的結構,我覺得是比較安全的把你的條件在上面......這可能工作,但每當我看到一個while循環的條件是在頂部,這是好事,嘗試遵循共同慣例。例如

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

不錯,你是正確的怎麼感謝您指出我的錯誤 –

+0

@JustinCapunoCuizon愚蠢的我:這是正常的,我們都做這些小錯誤不時。也請確保檢查我的解決方案作爲答案,以便其他用戶知道您的問題已得到解決。祝你今天愉快。 –

+0

我檢查它,但它說,等待5分鐘的xD –