1

我在一个 PHP 网站上有一个购物车,一切都很好,但我正在尝试创建一个函数,将购物车的内容通过电子邮件发送到指定的地址。

我的电子邮件工作完美,但我很难让购物车中的每件商品都显示出来。

到目前为止,我有下面的代码。问题在于我在 $email_body 变量中添加了一个 foreach 循环,但由于这是我显示购物车的方式,我认为这将是创建电子邮件的方式。谁能看到我哪里出错了?

提前致谢

<?php

session_start();

//echo "<br />CART: " . $_SESSION["cart"] . "<br />";

require_once('Connections/ships.php');

// Include functions

require_once('inc/functions.inc.php');

$cart = $_SESSION["cart"]; 

$visitor_email = $_POST['email'];

$arrCart = explode(",", $cart);



// Open db

mysql_select_db($database_ships, $ships);


// Grab first (and only) row from query

$row = mysql_fetch_array($cart_items);


//create the variables

$email_from = 'anemailaddress@gmail.com';//<== update the email address

$email_subject = "Ship Image Information Request";

$email_body = "You have received a request for Ship Information from:" . $visitor_email. "They would like information on the following ships" . 

"foreach ($arrCart as $cart_item) {


$sql = 'SELECT ship_id, ship_name, image FROM ship_infomation WHERE ship_id = $cart_item LIMIT 1;';

$cart_items = mysql_query($sql, $ships) or die(mysql_error()) $row['ship_name'];

}";

$to = "anemailaddress@gmail.com";//<== update the email address

$headers .= "Reply-To: $visitor_email \r\n";

//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.php');

?> 
4

1 回答 1

0

你想执行那个循环,PHP 代码,而不是把“foreach arrcart...”这个词放到你的邮件文本中。这就是您通过将其放入字符串中所做的事情。

//start creating the e-mail body string
$email_body = "You have received a request for Ship Information from: ";

//add to the email body string
$email_body .= $visitor_email . "\n\n";

//add to the email body string
$email_body .= "They would like information on the following ships:\n\n";

//begin a loop over the entries of $arrCart
foreach ($arrCart as $cart_item) {

    //the query to look up the cart item's name
    $sql = "SELECT ship_name FROM ship_infomation WHERE ship_id = $cart_item LIMIT 1";

    //run the database query
    $result = mysql_query($sql);

    //retrieve a row from the query result
    $row = mysql_fetch_array($result);

    //add a new line to the string in $email_body with the name we just looked up
    $email_body .= $row['ship_name'] . "\n";

}
于 2013-08-06T19:10:11.457 回答