Page 1 of 1
subtotalling values in array
Posted: Mon Sep 17, 2007 9:10 am
by kurtismonger
I'm trying to figure out how to sum values in an array. I have a database that tracks part defects and allows the user to pull data for a given department and date range. Each part number can have multiple defects. I can pull what I need from the database, but I need to iterate through the array and combine like elements. So for example if the report is run for a whole month there may be multiple types of the same defect for the same part. Here is what the data in my array looks like:
Part# / defectType / Defect Qty
101 / scratches / 10
101 / scratches / 15
101 / trim / 7
102 / scratches / 3
What I need is to print out a table that would show the following:
part # / defectType / Total
101 / scratches / 25
101 / trim /7
101 TOTAL SCRAP = 32
102 / scratches 3
102 TOTAL SCRAP = 3
TOTAL SCRAP = 35
Any help would be appreciated.
You should us a database but...
Posted: Mon Sep 17, 2007 10:37 am
by yacahuma
Hello,
My first guess is that you should be able to do it on the database, maybe from a second query just to get the stats. Although is another db call, it will make things simple. If you still want to do it on php you can do something like this.
Code: Select all
//i like to have all my data in classes
class DefectStat
{
var $partNum;
var $defectType;
var $quantity;
function DefectStat($p,$d,$q)
{
$this->partNum = $p;
$this->defectType = $d;
$this->quantity = $q;
}
}
// this functio is the one goinf to the database and getting the data
// just plug in you database calls there
function getMyData($params)//what ever you use to filter the data
{
$data = array();
// $data[] = new DefectStat($row['partNum'],'$row['defactType'],$row['quantity']);//example with database
//This is just so I can simulate the data
$data[] = new DefectStat(101,'scratches',10);
$data[] = new DefectStat(101,'scratches',15);
$data[] = new DefectStat(101,'trim',7);
$data[] = new DefectStat(102,'scratches',3);
return $data;
}
$datars = getMyData(1);
$stats = array();
foreach ($datars as $defect)
{
$stats[$defect->partNum]['total'] = isset ($stats[$defect->partNum]['total']) ? $stats[$defect->partNum]['total'] += $defect->quantity : $defect->quantity;
$stats[$defect->partNum][$defect->defectType] = isset ($stats[$defect->partNum][$defect->defectType]) ? $stats[$defect->partNum][$defect->defectType] += $defect->quantity : $defect->quantity;
}
print_r($stats);
?>
The output will be like this
Array ( [101] => Array ( [total] => 32 [scratches] => 25 [trim] => 7 ) [102] => Array ( [total] => 3 [scratches] => 3 ) )
So all your data is in stats
Code: Select all
echo "MY 101 TOTAL IS" . $stats[101]['total'];
echo "MY 101 scratches IS". $stats[101]['scratches'];
Posted: Mon Sep 17, 2007 10:55 am
by pickle
Posted: Mon Sep 17, 2007 11:29 am
by kurtismonger
Thanks yacahuma, thats a bit over my head. I get most of it until the foreach statement. Could you expand a bit on that?
I was able to write a query that essentially did what I needed, but using GROUP BY WITH ROLLUP prevents me from using ORDER BY and I need the final results sorted to order by highest defect quantity.
Posted: Mon Sep 17, 2007 12:04 pm
by kurtismonger
array_sum wont work since I have different items I need to sum on based on part number and defect type.