BИ Develop
BИ Develop

Reputation: 53

php foreach calculating sum from row

I have search and implements from many question in stackoverflow to my code but it always returning only the last row value. Here my code :

$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>';

How to fix this ?

Upvotes: 0

Views: 97

Answers (3)

F5 Buddy
F5 Buddy

Reputation: 494

        $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;

Upvotes: 0

Sandeep Singh
Sandeep Singh

Reputation: 41

It is not so efficient to do a sum in loop, better change your code as follows and to be done out side the loop as mentioned in question.

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

Upvotes: 0

Eddie
Eddie

Reputation: 26844

You are overding $totalpayment on every loop by this code:

$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>';

Upvotes: 2

Related Questions