1

I have a foreach loop that adds all lines from a table with dynamic rows (add row for every new product you want in the order from previous page - sometimes it could be 3 Products, sometimes it could be 23 Products) that creates a pdf purchase order (using mpdf). I'm not having any problem with the line items showing (Product x Quantity = Total) cost for each line. My issue is getting those totals into a single Total cost at the bottom of the Purchase order.

Example on the PDF created:

Product 1     $2 x 3 = $6
Product 2     $4 x 2 = $8
Product 3     $6 x 1 = $6

All that works correctly. Here is the code for the foreach loop.

   <?php

foreach ($product as $a => $b) {

    $prod_cost1[$a] = (("$qty[$a]") * ("$prod_cost[$a]"));

    echo "
        <tr>
            <td>$product[$a] &nbsp;</td>
            <td>$qty[$a] &nbsp;</td>
            <td>$prod_cost1[$a] &nbsp;</td>
        </tr>";
}

?>

I just somehow need to total all the $prod_cost1 and be able to display that somewhere on the page. Thank you for your help!

1
  • array_sum($prod_cost1) Commented Sep 26, 2018 at 14:21

2 Answers 2

2
  • Just create one more variable before the loop begins and increment it inside the loop.
  • It's a good habit to use proper typecasting when dealing with numbers (especially in a weakly typed language like PHP). So, I have typecasted the product cost to float.

You can use the following code:

// Initialize the total sum varaible
$total_sum = 0;

foreach($product as $a => $b) { 

    $prod_cost1[$a] = (("$qty[$a]") * ("$prod_cost[$a]"));

    // update the total sum (typecasting the product cost to float)
    $total_sum += (float)$prod_cost1[$a];

    echo"
         <tr>
         <td>$product[$a] &nbsp;</td>
         <td>$qty[$a] &nbsp;</td>
         <td>$prod_cost1[$a] &nbsp;</td>
         </tr>";
}

// access it
echo $total_sum;
Sign up to request clarification or add additional context in comments.

1 Comment

@Psam happy to help :)
0
$total = 0;
foreach($product as $a => $b) { 
    $prod_cost1[$a] = (("$qty[$a]") * ("$prod_cost[$a]"));

    $total += $prod_cost1[$a];

    echo"<tr>
        <td>$product[$a] &nbsp;</td>
        <td>$qty[$a] &nbsp;</td>
        <td>$prod_cost1[$a] &nbsp;</td>
    </tr>";
} 

echo "Total: {$total}";

6 Comments

Not me. But maybe lack of explanation! Also, yours is duplicate of mine :-)
@MadhurBhaiya I could say the same thing :) It happens often when racing to get there first.
@Barry there is a 1 minute gap. Anyways, I agree that you may not have copied my answer.
@Barry I am upvoting since yours is still a valid answer.
thanks @MadhurBhaiya I had the same thing earlier... other answer appeared same time as mine 12 votes and accepted... me nothing (at least no down vote)... all because I spent the extra time linking to docs, formatting, etc... which is why I didn't explain here I guess
|

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.