Page 1 of 1

foreach trouble

Posted: Mon Apr 12, 2010 7:07 pm
by vcarter
I have a do loop for customer order data - assuming each person ordered ONE item, their "order" would contain 3 lines of text, each with a header. The first line is "H" text, the second (which is the item information) line is "D" text, and the third line (which contains the total amount) is "T" text. So, for each customer, their information would be as such:

H tab delimited information about customer
D tab delimited information about item ordered
T tab delimited information about total order

However, if a customer ordered 3 items, then it would need to be like this:

H tab delimited information about customer
D tab delimited information about 1st item ordered
D tab delimited information about 2nd item ordered
D tab delimited information about 3rd item ordered
T tab delimited information about total order

I have having trouble with my foreach on the item information -- here's my current code:

Code: Select all

$items=array($row_rsCustomerOrders['sku_item_number']);

if ($totalRows_rsCustomerOrders) {
	do {
	fputs($handleWriteNM,"H\torder #\tcust order\tname\taddress 1\taddress 2\tcity\tstate\tzip code\tcountry\tphone number\temail\tshipping method\r\n"); // write 1st headers in output
	fputs($handleWriteNM,"H\t" . $row_rsCustomerOrders['invoice_number'] . "\t" . $row_rsCustomerOrders['order_id'] . "\t" . $row_rsCustomerOrders['shipping_first_name'] . ' ' . $row_rsCustomerOrders['shipping_last_name'] . "\t" . $row_rsCustomerOrders['shipping_address_1'] . "\t" . $row_rsCustomerOrders['shipping_address_2'] . "\t" . $row_rsCustomerOrders['shipping_city'] . "\t" . $row_rsCustomerOrders['shipping_state'] . "\t" . $row_rsCustomerOrders['shipping_zip'] . "\t" . $row_rsCustomerOrders['shipping_country'] . "\t" . $row_rsCustomerOrders['shipping_day_phone'] . "\t" . $row_rsCustomerOrders['marketplace_email'] . "\tUSPS\r\n");
	
	 foreach($items as $key => $value)
	 {
	fputs($handleWriteNM,"D\torder #\titem #\tdescription\tquantity\tprice\r\n"); // write 2nd headers in output
	fputs($handleWriteNM,"D\t" . $row_rsCustomerOrders['invoice_number'] . "\t" . $value . "\t" . $row_rsCustomerOrders['description'] . "\t" . $row_rsCustomerOrders['qty_won'] . "\t" . $row_rsCustomerOrders['unit_bid'] . "\r\n"); 	
	} 	
	
	fputs($handleWriteNM,"T\torder #\tsubtotal\tshipping\ttax\tdiscount\ttotal\r\n"); //write 3rd headers in output
	fputs($handleWriteNM,"T\t" . $row_rsCustomerOrders['invoice_number'] . "\t" . $row_rsCustomerOrders['shipment_subTotal'] . "\t" . $row_rsCustomerOrders['shipping_charge'] . "\t" . $row_rsCustomerOrders['total_tax'] . "\t" . $row_rsCustomerOrders['discount'] . "\t" . $row_rsCustomerOrders['total_invoice_amount'] . "\r\n");
	}
	while ($row_rsCustomerOrders=mysql_fetch_assoc($rsCustomerOrders));
}
Instead of just repeating that "D" line for each item number associated with a particular customer, it's still running through each - so for a customer who has 2 items, for instance, it's giving me this:

H
D
T
H
D
T

Instead of just

H
D
D
T

Hope that's clear...thanks in advance!

Virginia

Re: foreach trouble

Posted: Mon Apr 12, 2010 10:04 pm
by vcarter
So I now see it's doing more "wrong" than I originally thought...

Can someone show me any good examples for using foreach within a do loop? I can't seem to find many that are closer to my situation.

TIA

Virginia

Re: foreach trouble

Posted: Mon Apr 12, 2010 11:06 pm
by requinix

Code: Select all

for each person {
    print person information
    reset totals
    for each item {
        print item information
        adjust running totals
    }
    print totals
}
I don't know how you get more than one item. You declared $items with one value outside of the loop, and I don't see it changing anywhere inside.

Re: foreach trouble

Posted: Tue Apr 13, 2010 1:12 am
by omniuni
I think you should consider using separate database queries, rather than trying to parse out nested arrays.

Store the info to a database, then, query for the header, items, and generate your total information as needed.

Re: foreach trouble

Posted: Tue Apr 13, 2010 7:11 pm
by vcarter
tasairis wrote:

Code: Select all

for each person {
    print person information
    reset totals
    for each item {
        print item information
        adjust running totals
    }
    print totals
}
I don't know how you get more than one item. You declared $items with one value outside of the loop, and I don't see it changing anywhere inside.
Well, that's part of the trouble! I don't know either...and apparently I didn't. :) I'm still pretty new at this, and this particular project is rather intricate.

I'll study your recommendation - and figure out where to put my declaration for $items. :)

Thanks very much!

Re: foreach trouble

Posted: Tue Apr 13, 2010 7:12 pm
by vcarter
omniuni wrote:I think you should consider using separate database queries, rather than trying to parse out nested arrays.

Store the info to a database, then, query for the header, items, and generate your total information as needed.

Well, the information *is* in two separate tables. It's originally in one, and I pull it out and INSERT into two different tables (customer and orders) and link them by the invoice number. I'll get this eventually...hopefully. :)

Thanks for your reply.