I have 2 queries. One used as part of an outer loop and the other used in the inner loop.
I am using a foreach to move through the inner loop query values and build the body of an email. However, my results are producing repeating values. It makes sense why (multiple iterations through the foreach) but I don't know the right way to accomplish this.
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