Jump to content

Passing value from one MySQL query to another in nested PHP loop


feign3

Recommended Posts

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?

 

 

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      
     }
}

?>

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?

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

 

 

 

Archived

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

×
×
  • Create New...

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.