Darren Burgess
Darren Burgess

Reputation: 4312

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.

Upvotes: 0

Views: 3377

Answers (6)

Darren Burgess
Darren Burgess

Reputation: 4312

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

Upvotes: 0

Baba
Baba

Reputation: 95101

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

Upvotes: 1

Ozzy
Ozzy

Reputation: 8312

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"];

Upvotes: 1

heyanshukla
heyanshukla

Reputation: 669

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>

Upvotes: 1

dan-lee
dan-lee

Reputation: 14492

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

Upvotes: 2

Vipin Jain
Vipin Jain

Reputation: 1402

$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

Upvotes: 1

Related Questions