php通过电子邮件发送数组

php通过电子邮件发送数组,php,email,Php,Email,我试图通过电子邮件发送数组(购物车)中的结果,但是当我发送电子邮件时,电子邮件中显示的只是名字、价格和数量,而不是任何其他名称、价格或数量。我假设我的循环有问题,但不确定在哪里。非常感谢。结果放在逗号分隔的变量中 $result= mysqli_query($conn, "SELECT * FROM testtable "); // save product list as array if (mysqli_num_rows($result) > 0) { while($row =

我试图通过电子邮件发送数组(购物车)中的结果,但是当我发送电子邮件时,电子邮件中显示的只是名字、价格和数量,而不是任何其他名称、价格或数量。我假设我的循环有问题,但不确定在哪里。非常感谢。结果放在逗号分隔的变量中

$result= mysqli_query($conn, "SELECT * FROM testtable  ");

// save product list as array

if (mysqli_num_rows($result) > 0) {

while($row = mysqli_fetch_object($result)) 
{
$sku = $row->ProductSKU;
$productInfo[$sku] = array();
$productInfo[$sku]['Name'] = $row->Name;
$productInfo[$sku]['Price'] = $row->Price;
$productInfo[$sku]['QTY'] = $row->QTY;
}
} else {
die ("ERROR");
}

$i=0;
$total=0;
// print the currently selected items
foreach ($_SESSION['cart'] as $sku => $quantity) {


$subtotal = $quantity * $productInfo[$sku]['Price'];
$total += $subtotal;
$array = array("Product:".$productInfo[$sku]['Name'],
"Units @".$productInfo[$sku]['Price'] ,
"Total:".number_format($subtotal, 2));
$comma_separated = implode("\n", $array);
}

您可能应该更改以下代码:

// print the currently selected items
foreach ($_SESSION['cart'] as $sku => $quantity) {


$subtotal = $quantity * $productInfo[$sku]['Price'];
$total += $subtotal;
$array = array("Product:".$productInfo[$sku]['Name'],
"Units @".$productInfo[$sku]['Price'] ,
"Total:".number_format($subtotal, 2));
$comma_separated = implode("\n", $array);
}
进入:

之前,您已经创建了产品数组,使用新行将其内爆,但最后您没有将此数组保存在任何其他变量中,因此每次运行foreach循环时,您都会再次初始化
$comma\u separated
变量,这样以前的值就不存在了

现在,您应该在邮件内容中附加
$comma\u separated
,例如:

$mail_content .= $comma_separated;

您在哪里使用
$comma_separated
?我猜您需要将结果连接到
$comma_separated
,因为它在循环中每次都被过度写入。对不起,我是php新手,我的邮件正文是$body=wordwrap(“Order received$comma_separated”,70,“\r\n”);您的意思是现在应该是$body=wordwrap(“订单已收到$mail\u内容”,70,“\r\n”);不,您在这里显示的内容现在应该可以正常工作了:
$body=wordwrap(“订单已收到$逗号分隔”,70,“\r\n”)
它仍然只显示最后一个产品、价格和数量:(因此您应该检查
$\u会话['cart'的值是多少)
。这个变量中可能只有一个产品,这就是结果只有一个产品的原因。我将}放在后面代码的错误位置。非常感谢您的耐心等待。
$mail_content .= $comma_separated;