feign3 Posted February 10, 2009 Share Posted February 10, 2009 I am having a heck of a time figuring out how to pass a value from a query driving an outer loop to one driving the inner loop in a PHP page like so: while($row = mysql_fetch_assoc($CustomerDetails)) { Pass customer_id from $CustomerDetails query to $OrderDetails in inner loop below while($row = mysql_fetch_assoc($OrderDetails)) { Use customer_id value to drive this inner query } } The two queries are defined farther up in the code but the customer_id value I am trying to use for the second inner loop query seems to be out of scope. Does anyone have any examples of how this might work? Link to comment https://forums.phpfreaks.com/topic/144693-passing-value-from-one-mysql-query-to-another-in-nested-php-loop/ Share on other sites More sharing options...
metrostars Posted February 10, 2009 Share Posted February 10, 2009 This won't work as while($row = mysql_fetch_assoc($OrderDetails)) { can only be defined once. You should do <?php $result2 = mysql_fetch_array($OrderDetails); ?> and then <?php while($row = mysql_fetch_assoc($CustomerDetails)) { Pass customer_id from $CustomerDetails query to $OrderDetails in inner loop below foreach($result2 AS $key) { Use customer_id value to drive this inner query } } ?> Link to comment https://forums.phpfreaks.com/topic/144693-passing-value-from-one-mysql-query-to-another-in-nested-php-loop/#findComment-759267 Share on other sites More sharing options...
feign3 Posted February 11, 2009 Author Share Posted February 11, 2009 This won't work as while($row = mysql_fetch_assoc($OrderDetails)) { can only be defined once. You should do <?php $result2 = mysql_fetch_array($OrderDetails); ?> and then <?php while($row = mysql_fetch_assoc($CustomerDetails)) { Pass customer_id from $CustomerDetails query to $OrderDetails in inner loop below foreach($result2 AS $key) { Use customer_id value to drive this inner query } } ?> Ah! So I would assume that $key is the value I want to pass to the inner loop query... is that correct? Link to comment https://forums.phpfreaks.com/topic/144693-passing-value-from-one-mysql-query-to-another-in-nested-php-loop/#findComment-759339 Share on other sites More sharing options...
feign3 Posted February 11, 2009 Author Share Posted February 11, 2009 I was able to get a little further with this suggestion however I am having the following problem now. The below code is meant to retrieve some address information for a customer and then get all order details for that customer in an inner loop. while($row = mysql_fetch_assoc($CustomerDetails)) { $body .= "****************************************************************** \n"; $body .= "ORDER # ".$order_count."\n"; $body .= "****************************************************************** \n"; $body .= "NAME: ".$row["title"]." ".$row["first_name"]." ".$row["last_name"]."\n"; $body .= "SHIPPING ADDRESS: ".$row["address_1"]."\n"; $body .= "SHIPPING ADDRESS 2: ".$row_CustomerDetails["address_2"]."\n"; $body .= "CITY: ".$row["city"]."\n"; $body .= "STATE: ".$row["state"]."\n"; $body .= "ZIP: ".$row["zip"]."\n\n"; while ($row_OrderDetails = mysql_fetch_assoc($OrderDetails)){ foreach($row_OrderDetails AS $key) { $body .= "ITEM SKU: ".$key."\n"; $body .= "ITEM NAME: ".$key."\n"; $body .= "QUANTITY: ".$key."\n"; } } } This code however produces the following output: ITEM SKU: 10 ITEM NAME: 10 QUANTITY: 10 ITEM SKU: ANTI-AGING FACIAL SERUM ITEM NAME: ANTI-AGING FACIAL SERUM QUANTITY: ANTI-AGING FACIAL SERUM ITEM SKU: 2 ITEM NAME: 2 QUANTITY: 2 What would be the proper way to do this so I achieve this instead: ITEM SKU: 10 ITEM NAME: ANTI-AGING FACIAL SERUM QUANTITY: 2 Link to comment https://forums.phpfreaks.com/topic/144693-passing-value-from-one-mysql-query-to-another-in-nested-php-loop/#findComment-759420 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.