1

我想在我的 sendmail.php 中包含 foreach 循环 + echo $total 作为变量 $order 的输出。有人可以帮助我吗?我有点卡住了。

我的 sendmail.php:

<?php
if(!isset($_SESSION)) {
     session_start();
}

$to = $_SESSION['email'];
$firstname = $_SESSION['firstname'] ;
$lastname = $_SESSION['lastname'] ;
$email = $_SESSION['email'] ;
$addressline1 = $_SESSION['addressline1'] ;
$towncity = $_SESSION['towncity'] ;
$postcode = $_SESSION['postcode'] ;

foreach ($_SESSION['invoice'] as $value) { //needs to = $order
    echo $value."<br>";} //needs to = $order
echo "Total: $".$_SESSION['total']; //needs to = $order

//set subject
$subject = "Crystal Fusion - New Order";

//body of the e-mail
$body = "New Order Received:\n\n\n\n
    From: $firstname $lastname\n
    Email: $email\n
    Address: $addressline1\n
    Town/City: $towncity\n
    Postcode: $postcode\n
    Order: $order"; //needs to = foreach loop above

$sent = mail($to, $subject, $body);

if($sent)
    {echo "<script language=javascript>window.location = 'mail_succeed.php';</script>";}
else
    {echo "<script language=javascript>window.location = 'mail_fail.php';</script>";}
?>
4

2 回答 2

0

解决方案是使用类似的东西

ob_start(); 

// Add your output
foreach ($_SESSION['invoice'] as $value) { //needs to = $order
    echo $value."<br>";} //needs to = $order
echo "Total: $".$_SESSION['total']; //needs to = $order
// if you need your logic at multiple places, consider using a separate php file and including it here
// ...

$body=ob_get_contents();
ob_end_clean(); 
mail($to, $subject, $body);
于 2012-11-12T06:32:40.657 回答
-1

如何将你的 foreach 循环更改为:

$order = '';
foreach ($_SESSION['invoice'] as $value) { //needs to = $order
    echo $value."<br>"; //needs to = $order
    $order .= $value."\n";
}
echo "Total: $".$_SESSION['total']; //needs to = $order
$order .= "Total: $".$_SESSION['total'];
于 2012-11-12T06:06:46.790 回答