0

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.

5
  • 2
    you can add that codition in sql query only and get sum(yourfield) in query only. Commented Apr 16, 2012 at 10:20
  • Is that money you need to deal with? If so, take care of the floating point precision that will make this task something very serious if you need to be the sum correct. Commented Apr 16, 2012 at 10:27
  • I would stick to cent values with money calculations! It leaves every opportunity open (even it costs more bytes) Commented Apr 16, 2012 at 10:32
  • @JohnDoe don't forget to accept the answer which worked for you. Commented Apr 16, 2012 at 10:32
  • @heyanshukla i have accepted the method i went with and up-voted all for their help. Commented Apr 16, 2012 at 10:34

6 Answers 6

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

Comments

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

Comments

1

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>

Comments

1

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

Comments

1

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

Comments

0

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

1 Comment

lol He already said that he can use the sql command for that. All he wants is an alternative method so that he do not have to run the query again :D

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.