hpnotiqtymes Posted July 4, 2009 Share Posted July 4, 2009 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 4, 2009 Share Posted July 4, 2009 You need to do this in the database query using SUM() and GROUP BY Example SELECT field1, field2, agent_name, SUM(price) as price_total FROM table GROUP BY agent_name Quote Link to comment Share on other sites More sharing options...
hpnotiqtymes Posted July 4, 2009 Author Share Posted July 4, 2009 Thx. Ill give it a try. where it saids in the query Sum as price_total, just to make sure i get it in my memory banks, is just taking the sum and putting it into a temporary field called price_total that i can then call in the php script and do something like: while ( $row = mysql_fetch_object($result) ) { $temp->total = $row->price_total; } Quote Link to comment Share on other sites More sharing options...
hpnotiqtymes Posted July 4, 2009 Author Share Posted July 4, 2009 I just realized where using this method might be a problem. Please correct me if i dont know what the heck im talking about but as far as the query goes im already pulling the information using this query $query = "SELECT * FROM sales WHERE `Date` BETWEEN '$newStart' AND '$newEnd'"; i first pull from the db using a this query as i need the info for the current week. then i need to filter through the info to find the entries of the same agent then add up the numerical values. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 4, 2009 Share Posted July 4, 2009 Like I said, you want to accomplish this with your query. Here is what your query should look like (I am making an assumption that your agent and price fields are named accordingly - change them as needed. $query = "SELECT *, SUM(price) as price_total FROM sales WHERE `Date` BETWEEN '$newStart' AND '$newEnd' GROUP BY agent"; Now in your code which prits the results, use price_total instead of price Quote Link to comment Share on other sites More sharing options...
hpnotiqtymes Posted July 4, 2009 Author Share Posted July 4, 2009 I would like to say that u my friend are the man. It worked perfectly. I appreciate it I'm not used to doing things like that with queries. lol all the things Ive done with queries are really simple which is pretty much selecting all. lol the get between the 2 dates is probably the most complex query ive done which is pretty sad. 10 cool pts to you..again thx a bunch.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.