Jump to content

[SOLVED] Getting the SUM of output after query and conditional statements are met


sleepyw

Recommended Posts

This is somewhat of a continuation of a previous thread I had that never got resolved. I'm slowly chipping away at resolving it myself, but stuck on some calculations.

 

I am running a query, but before looping the output, it goes through a series of conditional statements before deciding which $ amount to show.

 

For the sake of this argument, the output table shows correctly and lists the dollar figures.

 

The output for each conditional statement is called $milestone_cost. The $milestone_cost values that fit the condition are then printed into the final HTML table.

 

How do I total up the output from everything that appears in the table as $milestone_cost. Seems like this would be easy, but anything i find online is building the SUM function into the query, which I can't do because of the complex conditions applied after the query runs.

 

Anyone got any ideas? Is there some info here I'm not providing to solve this?

 

Thanks in advance.

 

 

Link to comment
Share on other sites

You could do something like:

 

<?php
$sql = "SELECT *, SUM(cost) AS total_cost FROM table";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)){
echo "whatever";
}

$fin = mysql_fetch_assoc($res);
echo $fin['total_cost']; // total of all
?>

 

Link to comment
Share on other sites

So you're saying stick that code within the conditional statements for each item and echo $totalcost after the loop?

 

You were saying that you couldn't use the SUM statement in your SQL query.  As you loop through your result set and determine that a value needs to be added to the total, add it to a variable holding the total.

Link to comment
Share on other sites

I can't run the SUM in the query because there are several conditional statements after the query to filter out which costs are included in the final table. There is no way to do that in the query itself (unless I created separate queries for each condition, but then I'm back to my other thread asking how to combine results into a single output, which no one could answer).

Link to comment
Share on other sites

Maybe I'm not understanding the question correctly, but you should be able to hold the total cost in a variable:

 

$totalCost = 0;

while (<loop through db results>) {
    if (<condition>) {
        $totalCost += $cost;
    }
}

 

Brilliant - that worked. Thank you very much!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.