I have a database that tracks product defects. I can query the db and get a list of defects by part number for a particular department and date range. What I need to do is iterate through the returned query and count up the defects to get add up duplicate defect types and total defects by part number. I also need to sort the defect types by highest number of defects (for each part number) and display that in a table. Then I also want to count overall defect quantity. The same defect can occur multiple times for a given part number.
This is just example output, there could be up to 130+ types of defects for each part number.
This is the output of my query:
<PHP>
<TABLE cellpadding='0' cellspacing='0' border='0'>
<TR>
<TH>Dept</TH><TH>Part</TH><TH>Type</TH><TH>Defects</TH></TR>
<TR>
<TD>CNC</TD><TD>100</TD><TD>Handling</TD><TD>1</TD></TR>
<TR>
<TD>CNC</TD><TD>100</TD><TD>Scratches</TD><TD>1</TD></TR>
<TR>
<TD>CNC</TD><TD>101</TD><TD>Supplier Mold Defect</TD><TD>2</TD></TR>
<TR>
<TD>CNC</TD><TD>101</TD><TD>Handling</TD><TD>1</TD></TR>
<TR>
<TD>CNC</TD><TD>101</TD><TD>Scratches</TD><TD>7</TD></TR>
<TR>
<TD>CNC</TD><TD>101</TD><TD>Handling</TD><TD>6</TD></TR>
<TR>
<TD>CNC</TD><TD>101</TD><TD>Scratches</TD><TD>3</TD></TR>
<TR>
<TD>CNC</TD><TD>101</TD><TD>Base Run</TD><TD>4</TD></TR>
<TR>
<TD>CNC</TD><TD>101</TD><TD>Scratches</TD><TD>6</TD></TR>
<TR>
<TD>CNC</TD><TD>101</TD><TD>Supplier Mold Defect</TD><TD>9</TD></TR>
<TR>
<TD>CNC</TD><TD>101</TD><TD>Trimming</TD><TD>3</TD></TR>
<TR>
<TD>CNC</TD><TD>102</TD><TD>Base Run</TD><TD>4</TD></TR>
<TR>
<TD>CNC</TD><TD>102</TD><TD>Scratches</TD><TD>1</TD></TR>
<TR>
<TD>CNC</TD><TD>102</TD><TD>Scratches</TD><TD>7</TD></TR>
<TR>
<TD>CNC</TD><TD>102</TD><TD>Scratches</TD><TD>1</TD></TR>
<TR>
<TD>CNC</TD><TD>102</TD><TD>Handling</TD><TD>1</TD></TR>
<TR>
<TD>CNC</TD><TD>102</TD><TD>Scratches</TD><TD>3</TD></TR>
<TR>
<TD>CNC</TD><TD>102</TD><TD>Scratches</TD><TD>5</TD></TR>
<TR>
<TD>CNC</TD><TD>103</TD><TD>Scratches</TD><TD>13</TD></TR>
<TR>
<TD>CNC</TD><TD>103</TD><TD>Scratches</TD><TD>2</TD></TR>
<TR>
<TD>CNC</TD><TD>104</TD><TD>Scratches</TD><TD>10</TD></TR>
<TR>
<TD>CNC</TD><TD>105</TD><TD>Scratches</TD><TD>3</TD></TR>
<TR>
<TD>CNC</TD><TD>105</TD><TD>Trimming</TD><TD>1</TD></TR>
</TABLE>
<PHP>
So for example, For Part #103 I would want to sum the two entries for "Scratches" and print 15 and then provide a subtotal for #103 of 15. For part 105 I would return a count for scratches and one for Trimming and then a subtotal of 4.
I am really stuck here. Any help would be appreciated, even if its only a part of the solution. Not even sure I am asking this correctly, since I see a lot of reviews but not replies.
UPDATE:
Can't figure out why this is such an unusual problem that no one has seen before which is why I think I am asking the wrong question. I'll try to make it more generic:
I need to be able to walk through an array and sum and subtotal the elements in it without knowing what elements are going to be in the array beforehand. The array has the form:
part#/defectType/defectQty
As I stated above, the array could have multiple entries for each part number and there could by multiples of the same defectType and Part#.
101/scratches/5
101/scratches/3
101/Trim/19
102/scratches/12
102/Trim/3
For each part I want to subtotal defectTypes.