Php 如何将购物车的内容通过电子邮件发送回网络版主
由于我们销售的商品,我们不需要进行传统的购物车结帐,我们需要结帐按钮将购物车通过电子邮件发送回网络管理员,以便他批准我现在拥有的内容,它会将产品从数据库中取出,但当它发送电子邮件时,它会为购物车中的每个商品发送单独的电子邮件我想要一封电子邮件对于整个购物车来说,这很可能是一个循环问题,但似乎无法解决。如果您能提供任何帮助,我们将不胜感激`Php 如何将购物车的内容通过电子邮件发送回网络版主,php,html-email,shopping-cart,Php,Html Email,Shopping Cart,由于我们销售的商品,我们不需要进行传统的购物车结帐,我们需要结帐按钮将购物车通过电子邮件发送回网络管理员,以便他批准我现在拥有的内容,它会将产品从数据库中取出,但当它发送电子邮件时,它会为购物车中的每个商品发送单独的电子邮件我想要一封电子邮件对于整个购物车来说,这很可能是一个循环问题,但似乎无法解决。如果您能提供任何帮助,我们将不胜感激` foreach($_SESSION['shoppingCart'] as $key => $product) {
foreach($_SESSION['shoppingCart'] as $key => $product)
{
if (isset($_POST['checkout']))
{
$result = $this->qry("SELECT * FROM product WHERE Item_num=?", array($product['ID']));
foreach ($result as $row)
{
echo '<tr>';
echo '<td>'.$row['Name'].'</td>';
echo '<td>'.$product['Quantity'].'</td>';
echo '<td>'.$row['Price'].'</td>';
echo '<td>$'.number_format($product['Quantity'] * $row['Price'], 2).'</td>';
echo '<td><form method="POST">
<button type="submit" name="removeFromCart" value="'.$product['ID'].'">
Remove</button></form></td>';
echo '</tr>';
$total = $total + ($product['Quantity'] * $row['Price']);
$price= $row['Price'];
$lprice= $product['Quantity'] * $row['Price'];
$quan= $product['Quantity'];
$name = $row['Name'];
$quantity = $product['Quantity'];
$to = 'ms245@zips.uakron.edu';
$subject = 'order from fatfish aquatic website';
$message = "<html><body>";
$message .= "<table>";
$message .= "<th>Name</th><th>Quantity</th><th>Price</th> <th> line total</th>";
$message .= "<tr style='background: #eee;'><td>'.$name.'</td><td>'.$quan.'</td><td>'.$price.'</td><td>'.$lprice.'</td></tr>";
$message .="123";
$message .= "</table>";
$message .= $total;
$message .= $row['Name'];
$message .= 'shipping address <br>';
$message .= 'phone number <br>';
}
$message .= 'email<br>';
$message .= "</body></html>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ms245@zips.uakron.edu\r\n"."X-Mailer: php";
mail($to, $subject, $message, $headers);
}
}`
foreach($\u会话['shoppingCart']作为$key=>$product)
{
如果(isset($_POST['checkout']))
{
$result=$this->qry(“从产品中选择*项,其中项目数量=?”,数组($product['ID']);
foreach($结果为$行)
{
回声';
回显'.$row['Name'].';
回显'.$product['QUOTE'].';
回显'.$row['Price'].';
回显“$”.number_格式($product['QUOTE']*$row['Price'],2)。“”;
回声'
删除“;
回声';
$total=$total+($product['QUOTE']*$row['Price']);
$price=$row['price'];
$lprice=$product['Quantity']*$row['Price'];
$quan=$product['Quantity'];
$name=$row['name'];
$quantity=$product['quantity'];
$to$ms245@zips.uakron.edu';
$subject='从肥鱼水产网站订购';
$message=“”;
$message.=“”;
$message.=“NameQuantityPrice行总计”;
$message.=“'.$name'.$quan'.$price'.$lprice'.”;
$message.=“123”;
$message.=“”;
$message.=$total;
$message.=$row['Name'];
$message.=“发货地址
”;
$message.=“电话号码
”;
}
$message.=“电子邮件
”;
$message.=“”;
$headers='MIME版本:1.0'。“\r\n”;
$headers.=“内容类型:text/html;字符集=iso-8859-1”。“\r\n”;
$headers.=”来自:ms245@zips.uakron.edu\r\n““X-Mailer:php”;
邮件($to、$subject、$message、$headers);
}
}`
试试这个,我已经对代码进行了重构,这样您的邮件
呼叫将在您循环购物车并组装商品详细信息后发出。我不清楚您为什么会在代码中的这个关头重复“删除产品”表单-如果您正在制作并发送订单详细信息的最终电子邮件,那么订单一定已经下好了,对吗?您需要重新思考这方面的问题,但是我已经演示了如何重构电子邮件构建代码以获得您所要求的内容
if (!empty($_POST['checkout'])) {
// initialise message
$message = "<html><body>";
$message .= "<table>";
$message .= "<th>Name</th><th>Quantity</th><th>Price</th><th>line total</th>";
foreach ($_SESSION['shoppingCart'] as $key => $product) {
$result = $this->qry("SELECT * FROM product WHERE Item_num = ?", array($product['ID']));
foreach ($result as $row) {
// line item details added to message
$name = $row['Name'];
$quantity = $product['Quantity'];
$price = $row['Price'];
$lprice = $quantity * $price;
$total += $lprice;
$totalFormatted = number_format($total, 2);
$message .= "<tr style='background: #eee;'><td>'.$name.'</td><td>'.$quantity.'</td><td>'.$price.'</td><td>'.$lprice.'</td></tr>";
$message .= "123";
$message .= $name;
}
}
// finalise message
$message .= "</table>";
$message .= "<p id='checkout-summary'>";
$message .= "total: ${$totalFormatted} <br>";
$message .= 'shipping address <br>';
$message .= 'phone number <br>';
$message .= 'email<br>';
$message .= "</p>";
$message .= "</body></html>";
$to = 'ms245@zips.uakron.edu';
$subject = 'order from fatfish aquatic website';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ms245@zips.uakron.edu\r\n" . "X-Mailer: php";
mail($to, $subject, $message, $headers);
}
if(!empty($\u POST['checkout'])){
//初始化消息
$message=“”;
$message.=“”;
$message.=“NameQuantityPriceline总计”;
foreach($\会话['shoppingCart']作为$key=>$product){
$result=$this->qry(“从产品中选择*项,其中项目数量=?”,数组($product['ID']);
foreach($结果为$行){
//添加到消息的行项目详细信息
$name=$row['name'];
$quantity=$product['quantity'];
$price=$row['price'];
$lprice=$quantity*$price;
$total+=$lprice;
$totalFormatted=number\u格式($total,2);
$message.=“'.$name.'.$quantity.'.$price.'.$lprice.'”;
$message.=“123”;
$message.=$name;
}
}
//定稿信息
$message.=“”;
$message.=“”;
$message.=“总计:$;{$totalFormatted}
”;
$message.=“发货地址
”;
$message.=“电话号码
”;
$message.=“电子邮件
”;
$message.=“
”;
$message.=“”;
$to$ms245@zips.uakron.edu';
$subject='从肥鱼水产网站订购';
$headers='MIME版本:1.0'。“\r\n”;
$headers.=“内容类型:text/html;字符集=iso-8859-1”。“\r\n”;
$headers.=”来自:ms245@zips.uakron.edu\r\n““X-Mailer:php”;
邮件($to、$subject、$message、$headers);
}
<代码>邮件()/代码>函数在循环中…@米切尔SeaCurr,我已经为您提供了一个答案,如果您觉得这解决了您的问题,请将标记视为已接受的答案,谢谢:表中的行标题、行和表在代码中的每个行项和发货地址中重复出现在正文中,电话和电子邮件放在桌子外面的身体里?@Syfer桌子的标题不应该在那里,没错,是我的疏忽。但是,我们当然希望每个行项目都有一个新的
?至于正文中杂乱无章的摘要信息,这是根据他的源代码,虽然我已经用包装好了,但这不是由我来决定的。就是这样,不是一个代码编写服务,我只是简单地说明了他如何进行重构以解决他的实际问题:“它为购物车中的每一行项目发送一封单独的电子邮件-我希望整个购物车都有一封电子邮件”,我理解,但您可以指出错误(不要修复,只需指出)现在看起来可能是这样。我确实看到了——我的帖子顶部“我已经演示了如何重构电子邮件构建代码以获得您所要求的内容”,这并不是一个修复方案。您无法“修复”这样的问题-只需查看源代码即可!