Get sum of php array

I am adding elements from my SQL query to an associative array and printing them as is shown below;

foreach ($db->query($sql) as $row)
            {
?>      
                <p>
                <?php
                //Print details of each row 
                echo  "Sale Date: " . $row['saleDate'] . "<br />" . 
                      "Sale Price: " . $row['salePrice'] . "<br />" . 
                      "Buyers Email: " . $row['userEmail'] . "<br />";
                 ?> 
                </p>

i would like to be able to get the sum of salePrice for elements which match my SQL query. i know i can use the SQL sum command to get the sum of my whole salePrice column in sql, but i would only like to see the sum of the elements which match my query. Is there a simple way to do this?

Thanks.

Answers


$sum = 0;
foreach ($db->query($sql) as $row)
            {
?>      
                <p>
                <?php
                $sum +=  floatval($row['salePrice']);
                //Print details of each row 
                echo  "Sale Date: " . $row['saleDate'] . "<br />" . 
                      "Sale Price: " . $row['salePrice'] . "<br />" . 
                      "Buyers Email: " . $row['userEmail'] . "<br />";
                 ?> 
                </p>

Use this $sum variable for calculation


$summary = 0;
foreach ($db->query($sql) as $row) {
?>      
<p>
<?php
//Print details of each row 
echo  "Sale Date: " . $row['saleDate'] . "<br />" . 
                      "Sale Price: " . $row['salePrice'] . "<br />" . 
                      "Buyers Email: " . $row['userEmail'] . "<br />";
$summary += $row['salePrice'];
?> 
</p>
<?
}
echo 'Summary: '.$summary;
?>

you can add that codition in sql query only and get sum(yourfield) in query only. or else if you want to list all $row and sum the price of selected then

$sum = 0;
foreach ($db->query($sql) as $row)
        {
?>      
            <p>
            <?php
            if()// your condition here
            {
                $sum +=  floatval($row['salePrice']);
            }
            //Print details of each row 
            echo  "Sale Date: " . $row['saleDate'] . "<br />" . 
                  "Sale Price: " . $row['salePrice'] . "<br />" . 
                  "Buyers Email: " . $row['userEmail'] . "<br />";
             ?> 
            </p>

You can also do this simply in mysql (it may even be faster).

Assuming a data structure like this:

CREATE TABLE tbl_sales (
    buyers_email VARCHAR(50), /* probably should make this foreign key of tbl_buyer */
    sale_price DECIMAL(7,2),
    sale_date TIMESTAMP
);

An $sql query like this will do:

SELECT SUM(sale_price) AS total_sale FROM tbl_sales;

Then in PHP:

$query = $dbh -> exec ($sql);
$query -> setFetchMode(PDO::FETCH_ASSOC);
$results = $query -> fetchAll();

$total = $results[0]["total_sale"];

I think you should use the right tool for the right Job .. since you are calling MySQL it already has a SUM function

SELECT SUM(salePrice) as total 

That would get the Job done much faster


The simplest method by which to do this is to use the sql command.

SELECT SUM(salePrice) as sum

Then calling sum will give you the sum of salePrice for elements which match your associative array


Need Your Help

Use CoffeeScript class in AngularJS controller

javascript angularjs class coffeescript

I have a presenter class written in a CoffeeScript. It's in a file which is loaded before AngularJS controller. But I can't use this class there. How could I use it?