Jump to content


Photo

Trouble with array


  • Please log in to reply
6 replies to this topic

#1 steve m

steve m
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 25 October 2006 - 11:28 PM

Hi,

I'm in the process of writing a Stock report.  What I want to do is build a summary where it will display all the items and the quantity associated with that item. 
		while($row = mysql_fetch_array($result_model)){
		extract($row);
		$revenue=($cst + $revenue);
			$modelarray[] = $model_num;			
			
		}
							
			$model_count = array_count_values($modelarray);
						
				foreach ($model_count as $key => $value){

					echo "<tr><td width=\"200\"><p class=\"result\">$key</p></td>";
					echo "<td width=\"566\"><p class=\"result\">$value</p></td>";
				}

This works fine, but what I'm trying to do now, is while during the WHILE loop it is buliding an array for $model_num to get the quantity.  Is there a way during this that I can access the cost associated with those items so I can display the total for each item?


#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 October 2006 - 11:37 PM

I'm not sure what you're asking here, can you provide a sample row from your database and then give us an example of the figures, so we can work out what you're after.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 steve m

steve m
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 25 October 2006 - 11:48 PM

Sure,

The report would diplay somthing like this:

in the row from the database the model number below would have for example a cost of $7.50 per item.
Model Number            Quantity                  Total Cost
WT1200LE                  20                          I would like to have the $7.50 x Quantity (20)

The problem is  that I don't know how to get that cost from each row during the WHILE loop for each item.

Does this make sense?  Or did I make it more confusing?

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 26 October 2006 - 12:00 AM

ok, you didn't quite provide what I asked for, but that's ok, I'll make an assumption to help you.  I'm assuming from what you've said that a single row in your database will look like this:




Model NumberQuantityCost
WT1200LE207.50


Is that correct, I just want to establish what it looks like in your database, before I get my wellies on.

Regards
Huggie

Edit: I'm off to bed now, but this is easy to resolve, I'll pick it up in the morning :)
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#5 steve m

steve m
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 26 October 2006 - 12:23 AM

Sorry,  I knew I didn't explain it right.  The 20 is basically that there are 20 rows in the database that have the same model number. the example below is what I have to get that info and count how model numbers that are the same.

$modelarray[] = $model_num;


$model_count = array_count_values($modelarray);

adds up all of the model numbers that are the same to give the quantity (20 rows that have the same model number). So what I wanted to do is while the WHILE loop is going thru to collect the model numbers that are the same, I want to also be able to collect the cost for them in each row and be able to display that

For example each row for that model number has a cost of $7.50 and there are 20 rows that have the same model number.  I want to get that cost and just add it up.

Does this make more sense or did I just confuse you even more?

#6 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 26 October 2006 - 12:28 AM

OK, MySQL can do all of that for you, change your sql to something like this...

SELECT model_number, count(model_number) as quantity, sum(cost * quantity) as total_cost FROM parts GROUP BY model_number

Obviously you'll need your column names and table name in here.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#7 steve m

steve m
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 26 October 2006 - 12:34 AM

Sorry but the code didn't come out.  Can you please re-paste the code?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users