sleepyw Posted July 14, 2009 Share Posted July 14, 2009 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. Quote Link to comment Share on other sites More sharing options...
J.Daniels Posted July 14, 2009 Share Posted July 14, 2009 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; } } Quote Link to comment Share on other sites More sharing options...
sleepyw Posted July 14, 2009 Author Share Posted July 14, 2009 So you're saying stick that code within the conditional statements for each item and echo $totalcost after the loop? Quote Link to comment Share on other sites More sharing options...
marcus Posted July 14, 2009 Share Posted July 14, 2009 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 ?> Quote Link to comment Share on other sites More sharing options...
J.Daniels Posted July 14, 2009 Share Posted July 14, 2009 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. Quote Link to comment Share on other sites More sharing options...
sleepyw Posted July 14, 2009 Author Share Posted July 14, 2009 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). Quote Link to comment Share on other sites More sharing options...
sleepyw Posted July 14, 2009 Author Share Posted July 14, 2009 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.