steve m Posted October 25, 2006 Share Posted October 25, 2006 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. [code] 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>"; }[/code]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? Quote Link to comment https://forums.phpfreaks.com/topic/25108-trouble-with-array/ Share on other sites More sharing options...
HuggieBear Posted October 25, 2006 Share Posted October 25, 2006 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.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/25108-trouble-with-array/#findComment-114511 Share on other sites More sharing options...
steve m Posted October 25, 2006 Author Share Posted October 25, 2006 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 CostWT1200LE 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? Quote Link to comment https://forums.phpfreaks.com/topic/25108-trouble-with-array/#findComment-114516 Share on other sites More sharing options...
HuggieBear Posted October 26, 2006 Share Posted October 26, 2006 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:[table][tr][td]Model Number[/td][td]Quantity[/td][td]Cost[/td][/tr][tr][td]WT1200LE[/td][td]20[/td][td]7.50[/td][/tr][/table]Is that correct, I just want to establish what it looks like in your database, before I get my wellies on.RegardsHuggie[size=8pt][color=red][b]Edit:[/b][/color] I'm off to bed now, but this is easy to resolve, I'll pick it up in the morning :)[/size] Quote Link to comment https://forums.phpfreaks.com/topic/25108-trouble-with-array/#findComment-114518 Share on other sites More sharing options...
steve m Posted October 26, 2006 Author Share Posted October 26, 2006 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 thatFor 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? Quote Link to comment https://forums.phpfreaks.com/topic/25108-trouble-with-array/#findComment-114536 Share on other sites More sharing options...
HuggieBear Posted October 26, 2006 Share Posted October 26, 2006 OK, MySQL can do all of that for you, change your sql to something like this...[code]SELECT model_number, count(model_number) as quantity, sum(cost * quantity) as total_cost FROM parts GROUP BY model_number[/code]Obviously you'll need your column names and table name in here.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/25108-trouble-with-array/#findComment-114538 Share on other sites More sharing options...
steve m Posted October 26, 2006 Author Share Posted October 26, 2006 Sorry but the code didn't come out. Can you please re-paste the code? Quote Link to comment https://forums.phpfreaks.com/topic/25108-trouble-with-array/#findComment-114541 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.