2014-02-20 46 views
0
foreach ($retrievependingorders as $pendingorders){ 
     $orderno = $pendingorders['order_no']; 
     $lastupdate = $pendingorders['order_update_date']; 
     $orderstatus = $pendingorders['order_status']; 
     $totalprice = $pendingorders['total_price']; 

     echo "<tr><td>$orderno</td><td>$lastupdate</td><td>$orderstatus</td><td><center>$totalprice</center></td></tr>"; 
} 

夥計們,我有一個foreach循環將所有數據庫行回顯到表中。我是否可以知道如何總結掛單數組中存在的所有「總價」?php總結一個特定的數組內的值

+0

所以澄清$ grand_total_price + = $ totalprice是$ grand_total_price = $ grand_total_price + $ totalprice的簡稱 – Newbi3

回答

0

試試這個:

$total = 0; 
foreach ($retrievependingorders as $pendingorders){ 
     $orderno  = $pendingorders['order_no']; 
     $lastupdate = $pendingorders['order_update_date']; 
     $orderstatus = $pendingorders['order_status']; 
     $totalprice = $pendingorders['total_price']; 
     $total  += $totalprice; 

     echo "<tr><td>$orderno</td><td>$lastupdate</td><td>$orderstatus</td><td><center>$totalprice</center></td></tr>"; 
} 

echo $total; 
0

這是你如何能做到,採取VAR外循環,並添加所有的$totalprice吧!

$grand_total = 0 ; 

foreach ($retrievependingorders as $pendingorders){ 
     $orderno = $pendingorders['order_no']; 
     $lastupdate = $pendingorders['order_update_date']; 
     $orderstatus = $pendingorders['order_status']; 
     $totalprice = $pendingorders['total_price']; 
     $grand_total += $totalprice ; // Here we are summing the values 
     echo "<tr><td>$orderno</td><td>$lastupdate</td><td>$orderstatus</td><td><center>$totalprice</center></td></tr>"; 
} 
+0

明白了,謝謝你們。感謝您的幫助。 – user3322610

0

如果我理解正確的

foreach ($retrievependingorders as $pendingorders){ 
     $orderno = $pendingorders['order_no']; 
     $lastupdate = $pendingorders['order_update_date']; 
     $orderstatus = $pendingorders['order_status']; 
     $totalprice = $pendingorders['total_price']; 
     $sum_totalprice +=$totalprice 

     echo "<tr><td><a href=\"/enter-online-icas/my-orders/order-details/?orderno=".$orderno."\">$orderno</a></td><td>$lastupdate</td><td>$orderstatus</td><td><center>$totalprice</center></td></tr>"; 
} 
echo "<tr><td></td><td></td><td></td><td>".$sum_totalprice."</td></tr>"; 
0

你需要創建一個變量來保存的運行計數。

PHP

$grand_total_price = 0; //Initialize your variables 

foreach ($retrievependingorders as $pendingorders){ 
    $orderno = $pendingorders['order_no']; 
    $lastupdate = $pendingorders['order_update_date']; 
    $orderstatus = $pendingorders['order_status']; 
    $totalprice = $pendingorders['total_price']; 
    $grand_total_price += $totalprice; 

    echo "<tr><td>$orderno</td><td>$lastupdate</td><td>$orderstatus</td><td><center>$totalprice</center></td></tr>"; 

}

回聲 「我的總計:」。 $ grand_total_price。 「」;