Jump to content

[SOLVED] PHP Arrays logic


cleibesouza

Recommended Posts

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

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

 

 

 

<?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);
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.