Page 1 of 1

Filtering through an array for duplicates...lil help

Posted: Fri Jul 03, 2009 10:07 pm
by hpnotiqtymes
Hello all. I've got a question hopefully I can get a lil help on. Im sort of a PHP novice
Background:
I've created an app in flex that utilizes ZendAMF to make php calls to a db to pull information. the php queries the info it puts all the fields from each record into an object that then gets put into an array thats returned to flex which is then displayed in a datagrid. which Ive been able to get to work fine. 2 of the fields in each record are an agent name and a numerical amount.

Question:
there is a good chance that the agent name will appear more than once as a name variable in each object in the array but the numerical amount which is a price variable in the object could differ from object to object. What i want to do if possible is filter the array and throw out the duplicate object->name object but before disposing of them add all of the price variables of that particular agent, putting it into a total variable of the object and then returning that object so that flex will display in the data grid the agent name once but the total numerical number.

please let me know if this is possible of if this question isnt concise enough and there are questions on my question heh. Thanks to anyone that helps.

Re: Filtering through an array for duplicates...lil help

Posted: Sat Jul 04, 2009 12:21 am
by requinix
You can do it quite easily in your SQL query.

What is it right now?

Re: Filtering through an array for duplicates...lil help

Posted: Sat Jul 04, 2009 11:56 am
by hpnotiqtymes

Code: Select all

$query = "SELECT * FROM sales WHERE `Date` BETWEEN '$newStart' AND '$newEnd'";
that is the query at the moment. it needs to also pull from the current week which i have that working..

Re: Filtering through an array for duplicates...lil help

Posted: Sat Jul 04, 2009 3:44 pm
by requinix
In general you should only be SELECTing the fields you want.

You can use the GROUP BY clause and SUM function to group the results by person while summing up the prices.

Code: Select all

SELECT fields, person, SUM(price) AS price FROM sales WHERE `Date` BETWEEN '$newstart' AND '$newend' GROUP BY person
The results will also be sorted by person; if you don't want that add an ORDER BY to the query as well.