Filtering through an array for duplicates...lil help

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
hpnotiqtymes
Forum Newbie
Posts: 2
Joined: Fri Jul 03, 2009 9:54 pm

Filtering through an array for duplicates...lil help

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post by requinix »

You can do it quite easily in your SQL query.

What is it right now?
hpnotiqtymes
Forum Newbie
Posts: 2
Joined: Fri Jul 03, 2009 9:54 pm

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

Post 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..
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
Post Reply