Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/174148-array-foreach-i-think-help/
Share on other sites

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.

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 %

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.