php foreach计算行的总和

时间:2018-06-08 04:09:06

标签: php html

我已经从stackoverflow中的许多问题到我的代码进行搜索和实现,但它总是只返回最后一行的值。 这是我的代码:

$totalpayment = 0;
foreach($respon2 as $key => $row) {
    $name = $row['title'];
    $quantity = $row['quantity'];
    $price =  $row['price']*$row['quantity'];
    $totalpayment += $price;
    $price  = '$ ' . number_format($price) ;
    $totalpayment  = '$ ' . number_format($totalpayment) ;
    $mytable .= '<tr width="100%">';
    $mytable .= '<td  width="55%" align="left">' . $name . "</td>";
    $mytable .= '<td  width="20%" align="right">' . $quantity . "</td>";
    $mytable .= '<td  width="25%" align="right">' . $price . "</td>";
    $mytable .= '</tr>';
} 
$mytable .= '</table>';
$mytable .= '<table border="0" cellpadding="10" cellspacing="0" width="100%%" style="padding: 0px 25px 0px 25px;">';
$mytable .= '<tr><td width="45%"></td><td width="30%" class="table-title" align="right">Total Payment:</td><td width="25%" align="right">'. $totalpayment ."</td></tr>";
$mytable .= '</table>';

如何解决这个问题?

3 个答案:

答案 0 :(得分:2)

您通过此代码在每个循环上覆盖$totalpayment

$totalpayment = '$ ' . number_format($totalpayment) ;

$totalpayment = 0;
foreach($respon2 as $key => $row) {
    $name = $row['title'];
    $quantity = $row['quantity'];
    $price =  $row['price']*$row['quantity'];
    $totalpayment += $price;
    $price  = '$ ' . number_format($price) ;
    $mytable .= '<tr width="100%">';
    $mytable .= '<td  width="55%" align="left">' . $name . "</td>";
    $mytable .= '<td  width="20%" align="right">' . $quantity . "</td>";
    $mytable .= '<td  width="25%" align="right">' . $price . "</td>";
    $mytable .= '</tr>';
} 

$totalpayment  = '$ ' . number_format($totalpayment); //Try transferring this code outside the loop

$mytable .= '</table>';
$mytable .= '<table border="0" cellpadding="10" cellspacing="0" width="100%%" style="padding: 0px 25px 0px 25px;">';
$mytable .= '<tr><td width="45%"></td><td width="30%" class="table-title" align="right">Total Payment:</td><td width="25%" align="right">'. $totalpayment ."</td></tr>";
$mytable .= '</table>';

答案 1 :(得分:0)

在循环中进行求和并不是那么有效,如下所示更好地更改代码,并在循环中完成,如上所述。

  

$ prices = sum(array_coulmn($ respon2,'price'));   $ quantity = sum(array_coulmn($ respon2,'quantity'));   $ totalPayment = $ price * $ quantity;   $ totalPayment ='$'。 number_format($ totalPayment);

答案 2 :(得分:0)

        $totalpayment = 0;
        $mytable ='<table>';
        $respon2=array(array('title' =>'p1','quantity' =>2,'price' =>10),array('title' =>'p2','quantity' =>2,'price' =>20),array('title' =>'p3','quantity' =>1,'price' =>10));
        foreach($respon2 as $key => $row) {
        $name = $row['title'];
        $quantity = $row['quantity'];
        $price =  $row['price']*$row['quantity'];
        $totalpayment += $price;
        $price  = '$ ' . number_format($price) ;    
        $mytable .= '<tr width="100%">';
        $mytable .= '<td  width="55%" align="left">' . $name . "</td>";
        $mytable .= '<td  width="20%" align="right">' . $quantity . "</td>";
        $mytable .= '<td  width="25%" align="right">' . $price . "</td>";
        $mytable .= '</tr>';
        } 
        $totalpayment  = '$ ' . number_format($totalpayment) ;
        $mytable .= '</table>';
        $mytable .= '<table border="0" cellpadding="10" cellspacing="0" width="100%%" style="padding: 0px 25px 0px 25px;">';
        $mytable .= '<tr><td width="45%"></td><td width="30%" class="table-title" align="right">Total Payment:</td><td width="25%" align="right">'. $totalpayment ."</td></tr>";
        $mytable .= '</table>';

        echo $mytable;