I'm trying to take successful array results and start doing calculations on values based on another value in the array.
To start with, I want to sum every quantity 'TotalQTY' for each customer 'CSTNOC' in these arrays.
The array is printing fine but I'm getting undefined index errors in my last foreach loop.
I feel like this is simple enough but I'm not sure if my array isn't structure properly of if it's an issue with my loop.
Array
(
[0] => Array
(
[CSTNOC] => 1976
[FRAMEC] => 1051
[COVR1C] => 1150
[COLR1C] => 99
[START_DATE] => 2018-03-02
[TOTALQTY] => 2
)
[1] => Array
(
[CSTNOC] => 5400
[FRAMEC] => 1051
[COVR1C] => 1150
[COLR1C] => 99
[START_DATE] => 2017-11-10
[TOTALQTY] => 1
)
[2] => Array
(
[CSTNOC] => 5400
[FRAMEC] => 1051
[COVR1C] => 1150
[COLR1C] => 99
[START_DATE] => 2017-04-07
[TOTALQTY] => 2
)
[3] => Array
(
[CSTNOC] => 5400
[FRAMEC] => 1051
[COVR1C] => 1150
[COLR1C] => 99
[START_DATE] => 2018-02-09
[TOTALQTY] => 2
)
[4] => Array
(
[CSTNOC] => 11316
[FRAMEC] => 1051
[COVR1C] => 1150
[COLR1C] => 99
[START_DATE] => 2017-03-03
[TOTALQTY] => 1
)
I basically want these results for an excel report purpose:
CSTNOC | TotalQTY
1976 | 2
5400 | 5
11316 | 1
This is the portion of the script:
$dealerQuery = "
SELECT
cstnoc,
framec,
covr1c,
colr1c,
cast(Left(extd2d, 4)||'-'||substring(extd2d,5,2)||'-'||substring(extd2d, 7,2) as date) as start_date,
sum(orqtyc) as TotalQTY
from table
where cstnoc = {$skuRow['dealer_id']}
AND framec = {$skuRow['frame']}
AND colr1c = {$skuRow['color']}
AND covr1c = {$skuRow['cover']}
AND extd2d >= " . str_replace('-', '', $skuRow['start_date']) . "
group by cstnoc, framec,covr1c,colr1c,extd2d
";
$dealerRslt = odbc_exec($DB2Conn, $dealerQuery);
foreach($skuResult as $skuRow){
while($dealerRow = odbc_fetch_array($dealerRslt)){
$dealerResult[] = $dealerRow;
$sum = 0;
foreach($dealerResult['cstnoc'] as $dealerRow){
$sum += $dealerRow['TotalQTY'];
}
echo $sum;
}
}