paruby Posted September 14, 2009 Share Posted September 14, 2009 I have data that is based on a key->value scenario. My hope is this - add up all the "values" that have the same "key". I have code like :: $result = mysql_query("SELECT * FROM table) while ($resultsRow = mysql_fetch_array($results)) { $key = $resultsRow['typeid']; $value = $resultsRow['value']; $pairs["$value"] = $key; } foreach($pairs AS $value => $key) { .... add each $value together that have the same $key. So if I had something like array(1->2, 1->5, 1->8, 2->5, 2->7), I could arrive at array(1->15, 2->12). I saw "array_sum", but it isn't quite what I am looking for... Is this at all possible? Thanx! Pete Quote Link to comment https://forums.phpfreaks.com/topic/174148-array-foreach-i-think-help/ Share on other sites More sharing options...
kratsg Posted September 14, 2009 Share Posted September 14, 2009 Since you're using a query, why not let the query sum this for you? mysql_query("SELECT SUM('value') AS 'sum' FROM `table` GROUP BY `typeid`"); Quote Link to comment https://forums.phpfreaks.com/topic/174148-array-foreach-i-think-help/#findComment-918017 Share on other sites More sharing options...
corbin Posted September 14, 2009 Share Posted September 14, 2009 Since you're using a query, why not let the query sum this for you? mysql_query("SELECT SUM('value') AS 'sum' FROM `table` GROUP BY `typeid`"); Errr.... I think that SUM('value') will always be 0 since the string 'value' will be casted to an int. Also, I think with aliases, ` is used, not '. Could be wrong though. Quote Link to comment https://forums.phpfreaks.com/topic/174148-array-foreach-i-think-help/#findComment-918019 Share on other sites More sharing options...
paruby Posted September 14, 2009 Author Share Posted September 14, 2009 Oh, I messed up, sorry everyone. The key and value pairs are NOT from the same MySQL call, but separate values gotten from 2 separate DB calls. And the 2 tables are not directly related, but related between a 3rd table (not sure if that matters). Let me give the real case scenario - I have items with prices, and the items may or may not be taxable, and there are multiple tax %. So $key represents the ID of the tax amount table, and $value is the price of the item. So I want to add up all the items w/ taxID = 1, then apply the tax %, and add up all items w/ taxID = 2, and then apply that tax % Quote Link to comment https://forums.phpfreaks.com/topic/174148-array-foreach-i-think-help/#findComment-918021 Share on other sites More sharing options...
kratsg Posted September 14, 2009 Share Posted September 14, 2009 Since you're using a query, why not let the query sum this for you? mysql_query("SELECT SUM('value') AS 'sum' FROM `table` GROUP BY `typeid`"); Errr.... I think that SUM('value') will always be 0 since the string 'value' will be casted to an int. Also, I think with aliases, ` is used, not '. Could be wrong though. I probably wasn't looking closely >.< I have problems sometimes between { and [ if the font is small enough. You're probably right though. Oh, I messed up, sorry everyone. The key and value pairs are NOT from the same MySQL call, but separate values gotten from 2 separate DB calls. And the 2 tables are not directly related, but related between a 3rd table (not sure if that matters). Let me give the real case scenario - I have items with prices, and the items may or may not be taxable, and there are multiple tax %. So $key represents the ID of the tax amount table, and $value is the price of the item. So I want to add up all the items w/ taxID = 1, then apply the tax %, and add up all items w/ taxID = 2, and then apply that tax % You can probably still do this all in one query (complicated, possibly, but still one query). Can you show us screenshots of the tables in question? We might be able to help you with the query. I'm having a hard time based on that explanation alone though >.< Quote Link to comment https://forums.phpfreaks.com/topic/174148-array-foreach-i-think-help/#findComment-918345 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.