我正在尝试通过电子邮件发送数组(购物车)中的结果,但是当我发送电子邮件时,它只是电子邮件中显示的第一个名称,价格和数量,而不是任何其他名称,价格或数量'第我假设我的循环有问题,但不确定在哪里。提前谢谢了。结果放在comma_separated变量
中$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);
}
答案 0 :(得分: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
$comma_separated = array();
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(', ',$array);
}
$comma_separated = implode("\n", $comma_separated);
之前你已经创建了产品数组,使用新行破坏了它,但最后你还没有将这个数组保存在任何其他变量中,所以每次运行foreach循环时你都会再次初始化$comma_separated
变量,所以之前的价值不在那里。
现在您应该将$comma_separated
附加到邮件内容,例如:
$mail_content .= $comma_separated;