Hmmm? Okay, let’s go back a bit. I reread your original post. I read it too fast and thought you just wanted to dump the dups, not total them.
Normally in a database system you would use your QUERY to handle this. Before starting any printout
of the data, you would use something like “GROUP by order_item_part ORDER by” or whatever would
be correct for your code. Then, you would add in “COUNT (some field)” to add in a total for the correct
items. Anyway, to do this with an array is just a bit more tricky. Since you do have it down to two items,
the name and values, it should be easy enough to sort out.
So, your original code:
[php]
<?php foreach (@$items as $item) : ?>
<?php
$model->setState( 'filter_orderitemid', $item->orderitem_id);
$attributes = $model->getList();
foreach($attributes as $attribute) {
if(!empty($attribute->orderitemattribute_value)) {
echo $attribute->orderitemattribute_name.' : '.$attribute->orderitemattribute_value;
echo '
'. ' : Quanity: ' .'';
echo count($attribute->orderitemattribute_value);
echo array_count_values($attribute);
echo '
';
}
}
[/php]
In that code, you loop thru the "$attributes" array and handle everything inside it.
To create totals from that array while dumping duplicates, you would need to parse thru
the array for unique items and then reparse them to handle the totals. Maybe someone
else has a better idea?
The following is basically similar to the old code, but, first parses thru looking for a part name.
If the part already exists, in a temporary array, it skips it. If it is a new part name, it totals up
all of the similar names creating a total. In this way, when it finds a new name, it keeps track of
it and grabs the total for it. Then, it prints the part name and grand total for all of the same part
names. Then, it moves to the next one. If the name is already in the temp list, it has already been
totaled and we can just skip it. Hope that makes sense. And, hope the code works as I can not
test it without setting up a lot of data first... Give this a try and let us know if it does what you need.
[php]
<?php foreach (@$items as $item) : ?>
<?php
$model->setState( 'filter_orderitemid', $item->orderitem_id);
$attributes = $model->getList();
// New code, parses thru attributes array keeping track of dups and totals...
$temp_attributes = array();
foreach($attributes as $attribute) {
if (!array_key_exists($attribute->orderitemattribute_name, $temp_attributes.'
// New part-name, add it to temp array and save the value...
$temp_attributes[$attribute->orderitemattribute_name] = $attribute->orderitemattribute_value;
} else {
// Duplicate part-name, add it's value to existing one...
$temp_attributes[$attribute->orderitemattribute_name] .= $attribute->orderitemattribute_value;
}
}
// Print attribute names and values using the temporary array... (And, total all values!)
$total_value = 0;
foreach($temp_attributes as $attribute_name=>$attribute_value) {
echo $attribute_name.' : ' . $attribute_value;
$total_value .= $attribute_value;
}
echo '
'. ' : Quanity: ' . $total_value . '';
[/php]
Note: Basically, I just made it a two pass process. The first creates a temp array with name and value and totals all similar ones. Then, it prints that one. It should work for you... Let us know...