subtotalling values in array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kurtismonger
Forum Newbie
Posts: 3
Joined: Mon Sep 17, 2007 8:59 am

subtotalling values in array

Post 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.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

You should us a database but...

Post 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'];
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
kurtismonger
Forum Newbie
Posts: 3
Joined: Mon Sep 17, 2007 8:59 am

Post 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.
kurtismonger
Forum Newbie
Posts: 3
Joined: Mon Sep 17, 2007 8:59 am

Post by kurtismonger »

pickle wrote:array_sum()
array_sum wont work since I have different items I need to sum on based on part number and defect type.
Post Reply