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? Quote Link to comment 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 } } ?> Quote Link to comment 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? Quote Link to comment 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 Quote Link to comment 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.