cleibesouza Posted September 30, 2008 Share Posted September 30, 2008 Kind'a stuck on a problem. Please bear with me. I'm bringing data from 2 dbs and summing them up. The data comes in the format: CREDIT NAME - CREDIT AMOUNT I display all credits and also display a little summary of each data source. My trouble started when I was tasked with adding an overall summary to the page. So far I have: (remember that I'm bringing data from 2 different data sources) data source 1: CREDIT NAME: CREDIT AMOUNT: CEU 4.50 Contact Hours 29.00 data source 2: CREDIT NAME: CREDIT AMOUNT: CEU 10.60 NCSAPPB GSB 3.00 Contact Hours 15.00 The individual summaries are working the way they're supposed to. What I need now is an overall summary where if credit names are the same for data source 1 and data source 2, they get summed up but only show up once on the overall summary. Something like this for the above data: OVERALL SUMMARY: CREDIT NAME: CREDIT AMOUNT: CEU 15.10 (which is the sum of data source 1 + data source 2) Contact Hours 44.00 (which is the sum of data source 1 + data source 2) NCSAPPB GSB 3.00 (only shows up on data source 2) Any help will be GREATLY appreciated. Link to comment https://forums.phpfreaks.com/topic/126504-solved-php-arrays-logic/ Share on other sites More sharing options...
Barand Posted September 30, 2008 Share Posted September 30, 2008 Current code would be GREATLY appreciated. Link to comment https://forums.phpfreaks.com/topic/126504-solved-php-arrays-logic/#findComment-654180 Share on other sites More sharing options...
cleibesouza Posted September 30, 2008 Author Share Posted September 30, 2008 These are the queries that get me the individual summaries: $summary1 = "SELECT CAST(SUM(credit_hours) AS VARCHAR(10)) AS sum1CreditAmount, credit_name FROM myViewNameGoesHere WHERE person_id = '$idVariableGoesHere' GROUP BY credit_name ORDER BY credit_name"; $summary2 = "SELECT TRIM(CAST(SUM(CreditAmount) AS CHAR(10))) AS sum2CreditAmount, CreditName FROM tb1 te, tb2 tc, tb3 cr WHERE AHECUserID = '$userIdVariableGoesHere' AND te.EventKey = tc.EventKey AND tc.CreditCode = cr.CreditCode GROUP BY CreditName ORDER BY CreditName"; Link to comment https://forums.phpfreaks.com/topic/126504-solved-php-arrays-logic/#findComment-654189 Share on other sites More sharing options...
Barand Posted September 30, 2008 Share Posted September 30, 2008 <?php $summary1 = "SELECT SUM(credit_hours) AS sum1CreditAmount, credit_name FROM myViewNameGoesHere WHERE person_id = '$idVariableGoesHere' GROUP BY credit_name ORDER BY credit_name"; $res = mysql_query ($sql); $results = array(); while (list ($amount, $name) = mysql_fetch_row($res)) { $results[$name] = $amount; } $summary2 = "SELECT SUM(CreditAmount) AS sum2CreditAmount, CreditName FROM tb1 te, tb2 tc, tb3 cr WHERE AHECUserID = '$userIdVariableGoesHere' AND te.EventKey = tc.EventKey AND tc.CreditCode = cr.CreditCode GROUP BY CreditName ORDER BY CreditName"; $res = mysql_query ($sql); while (list ($amount, $name) = mysql_fetch_row($res)) { if (isset($results[$name])) $results[$name] += $amount; // add results from second query else $results[$name] = $amount; // create new element with amount. } foreach ($results as $name => $amount) { printf ('%s : %10.2f', $name, $amount); } ?> Link to comment https://forums.phpfreaks.com/topic/126504-solved-php-arrays-logic/#findComment-654319 Share on other sites More sharing options...
cleibesouza Posted October 1, 2008 Author Share Posted October 1, 2008 Thank you very much. This solved the problem. Link to comment https://forums.phpfreaks.com/topic/126504-solved-php-arrays-logic/#findComment-654727 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.