Jump to content

Archived

This topic is now archived and is closed to further replies.

steve m

Trouble with array

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Regards
Huggie

[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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Regards
Huggie

Share this post


Link to post
Share on other sites

×

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.