0

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 ?

1
  • Simply typo, you are overwriting your variable. Commented Jun 8, 2018 at 4:14

3 Answers 3

2

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>';
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it's my fault
Happy to help :)
0

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

1 Comment

Did you mean array_sum(array_column($respon2, 'price ')); ? But your code giving the wrong calculating of total payment
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;

1 Comment

it was already answered bro, i just write some part of my code. the purpose of my full code is for print the html to pdf file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.