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?

 

 

Link to comment
Share on other sites

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

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.