Eliminate Duplicates and Counts

I am using Joomla with the J2Store extension.

I am trying to edit the code to count the number of parts of the items in an order.

This code lists out the the part name and the part value.

The problem that I am having is that it prints out all the parts, even the duplicates.

What I want to do is only print out the parts once and count the parts.

<?php JModelLegacy::addIncludePath(JPATH_SITE.'/components/com_j2store/models'); $model = JModelLegacy::getInstance( 'OrderItemAttributes', 'J2StoreModel' ); ?> <?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 endforeach; ?>

Well, you have a list of items. But, this array is actually a multidimensional array.
If it was a single array, you could just use the function “unique()” which would work great.
But, in an array of arrays, you have to use the unique() function just for the column that holds
the part value. This is a bit tricky. But, it has been solved before.

You will have to sort it out by testing it. But, here is a link that talks about it and shows the
routine needed. You just have to tell the routine to use the part value somehow.

Might work… Good luck!
http://stackoverflow.com/questions/8923406/php-removing-elements-from-an-array-that-have-duplicates-values-on-a-specified

Alright I am trying to figure this out. Php is pretty new to me I haven’t did too much with it.

I followed the link that you posted and came up with this.

This prints out “Select” 22 times. Which I originally had the part and value print out 22 times.

Do i need to somehow take the array arrs and pull out the value for each spot in the array for all the values?

Or am i not even on the right track?

[php]
<?php
	JModelLegacy::addIncludePath(JPATH_SITE.'/components/com_j2store/models');
	$model = JModelLegacy::getInstance( 'OrderItemAttributes', 'J2StoreModel' );
?>	

<?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)) {
								
					
               /* ADDED THIS  */
                                      $result = array();
					foreach($attribute as $arrs){
						if(!isset($result[$arrs["orderitemattribute_value"]])){
							$result[$arrs["orderitemattribute_value"]] = $arrs;
							
							
						}
					}
										
					echo $arrs;
				/* THIS ONLY PRINTS OUT 'SELECT' 22 TIMES */


			
			/*             What it used to print out.  The 22 part names and values.
			echo $attribute->orderitemattribute_name.' : '.$attribute->orderitemattribute_value;
			*/
			
			echo '<br />';
			
		}	
		
	}
	
	?>	
	
<?php endforeach; ?>
[/php]

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...
Sponsor our Newsletter | Privacy Policy | Terms of Service