Jump to content


Photo

Adding to existing elements in an array


  • Please log in to reply
6 replies to this topic

#1 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 27 September 2006 - 05:15 PM

I am trying to count up how many instances something happens.

I have an query that pulls values from a table.  The values are Items and Qty.  What I'm trying to do is create an array of the Items and their total quantities.

<?php
$UNITS = array();

$UNITS[$result['Item']] .= $result['Qty'];

?>

Now this just adds the number to the number that already exists in the array.

How do I add the values of waht is in the array to the new value and put it back into the array?

List is something like:

Widget1  - 14
Widget2  - 3
Widget2 - 5
Widget3 - 2
Widget1 - 6

So I would have:

20 Widget1s
8 Widget2s
2 Widget3


#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 27 September 2006 - 05:18 PM

why not do that in your query?
SELECT SUM(qty) AS qty, item FROM table GROUP BY item;

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 27 September 2006 - 05:32 PM

I'd have to do a seperate query.

Right now my query is this:

SELECT * , COUNT(*) FROM RMAs GROUP BY RMANumber

There can be multiple items for a single RMANumber. Can I somehow incorporate the SUM in to my SELECT?


#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 27 September 2006 - 05:35 PM

if you're summing the same thing you're counting, sure:
SELECT RMANumber, COUNT(RMANumber) AS num, SUM(qty) AS qty FROM table GROUP BY RMANumber

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 27 September 2006 - 05:45 PM

Maybe I explained it wrong or I'm a bit confused.

an RMA entry might look like this

Line #    |    Unit      |RMANumber    | Notes
----------------------------------------------
1            | Widget1  | 9090999        | Blah
----------------------------------------------
2            | Widget2  | 9090999        | Mroe Blah
----------------------------------------------
3            | Widget1  | 9090999        | WOo Blah
----------------------------------------------

So in the RMA above it has 2x Widget1s and 1x Widget2s

My main query looks like this:

SELECT *, COUNT(*) FROM RMAs WHERE Received >= '{$MyDate}' AND Received != '0000-00-00 00:00:00' GROUP BY RMANumber
Where MyDate is a user inputed date like 2006-06-01 00:00:01

Then I loop through the result to print out entries and group them by their RMANumbers and output them to the screen. At the same time I need a running total of how many Units of each time this loops sees.





#6 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 27 September 2006 - 05:48 PM

ah, ok, i think i see... so, something like this might help:
<?php
$totals = array();
while ($x = mysql_fetch_array($sql)) {
  $item = $x['unit'];
  if (isset($totals[$item])) $totals[$item]++; // increment it since it's already set
  else $totals[$item] = 1; // otherwise, set it to one since it's the first one we've seen
}
?>

does that help at all?
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#7 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 27 September 2006 - 06:02 PM

That makes sense. I think because I am grouping it by RMANumber it won't see the individual items, so I'll have to do another query after this one and then use your above code to tally the items up.

Thanks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users