购物车的PHP电子邮件没有被循环填充

时间:2012-11-20 04:31:12

标签: php function email foreach

我想要做的是从网站生成购物车,并通过电子邮件发送。当我运行我的代码时,该功能在页面上运行并且不通过电子邮件发送,但它正确显示。我无法将我的函数的输出存储到将在电子邮件中工作的变量。这是我的代码......

function emailcartview() {
$total=0;
$totalvalue=0;
foreach($_SESSION as $name => $value) {
    if ($value>0){
        if (substr($name, 0, 5)=='cart_'){
            $id = substr($name, 5, (strlen($name)-5));
            $get = mysql_query('SELECT id, name, cost, item_number FROM mydbname WHERE id='.mysql_real_escape_string((int)$id));


            $cart = array();
            while ($get_row = mysql_fetch_assoc($get)) {
                $cart[] = array(
                'hsid' => $get_row['id'],
                'cost' => $get_row['cost'],
            $cost = $get_row['cost'],
                'itemno' => $get_row['item_number'],
                'name' => $get_row['name'],
                'value' => $value,
                $sub = $value*$cost,
                $total += $sub,
                $totalvalue = $value,
                'realsub' => number_format($sub, 2),
                );

                }
                foreach($cart as $index => $record){
                    global $emailcart;
                    $emailcart = "Item: {$record['itemno']} - {$record['name']} | Qty: {$record['value']} | Cost: {$record['cost']} x {$record['value']} = {$record['realsub']} <br>";
                    echo $emailcart;


            }

        }

    }

}
echo "Total : $".number_format($total, 2);
 }


$emailSubject = 'Invoice Submission';
$webMaster = 'my_email@blahblah.com';


$companyName = $_POST['companyname'];
$contactName = $_POST['contactname'];
$contactEmail = $_POST['contactemail'];
$teleNumber = $_POST['telenumber'];
$billAddr = $_POST['billaddr'];
$shipAddr = $_POST['shipaddr'];
$neededBy = $_POST['neededby'];
$cartprint .= emailcartview();

$body = "
<br><hr><br>
Company Name: $companyName <br>
Contact Name: $contactName <br>
Contact Email: $contactEmail <br>
Telephone Number: $teleNumber <br>
Billing Address: $billAddr <br>
Shipping Address: $shipAddr <br>
Date Needed: $neededBy <br> <br>
<br><hr><br>
$cartprint";

$headers = "From: ordersubmission@blahblah.com\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);

 ?>

可能有很多奇怪的东西已经尝试过并且没有工作,而不是最干净的。非常感谢任何帮助!!

泰勒。

1 个答案:

答案 0 :(得分:1)

在函数emailcartview中,你需要生成字符串而不是echo语句,在函数结束时你应该返回该字符串。

检查以下代码:

function emailcartview() {
$total=0;
$totalvalue=0;
$str = '';
foreach($_SESSION as $name => $value) {
    if ($value>0){
        if (substr($name, 0, 5)=='cart_'){
            $id = substr($name, 5, (strlen($name)-5));
            $get = mysql_query('SELECT id, name, cost, item_number FROM mydbname WHERE id='.mysql_real_escape_string((int)$id));


            $cart = array();
            while ($get_row = mysql_fetch_assoc($get)) {
                $cart[] = array(
                'hsid' => $get_row['id'],
                'cost' => $get_row['cost'],
            $cost = $get_row['cost'],
                'itemno' => $get_row['item_number'],
                'name' => $get_row['name'],
                'value' => $value,
                $sub = $value*$cost,
                $total += $sub,
                $totalvalue = $value,
                'realsub' => number_format($sub, 2),
                );

                }
                foreach($cart as $index => $record){
                    global $emailcart;
                    $str. = "Item: {$record['itemno']} - {$record['name']} | Qty: {$record['value']} | Cost: {$record['cost']} x {$record['value']} = {$record['realsub']} <br>";

            }

        }

    }


}
$str. = "Total : $".number_format($total, 2);
return $str;
 }


$emailSubject = 'Invoice Submission';
$webMaster = 'my_email@blahblah.com';


$companyName = $_POST['companyname'];
$contactName = $_POST['contactname'];
$contactEmail = $_POST['contactemail'];
$teleNumber = $_POST['telenumber'];
$billAddr = $_POST['billaddr'];
$shipAddr = $_POST['shipaddr'];
$neededBy = $_POST['neededby'];
$cartprint .= emailcartview();

$body = "
<br><hr><br>
Company Name: $companyName <br>
Contact Name: $contactName <br>
Contact Email: $contactEmail <br>
Telephone Number: $teleNumber <br>
Billing Address: $billAddr <br>
Shipping Address: $shipAddr <br>
Date Needed: $neededBy <br> <br>
<br><hr><br>
".emailcartview();

$headers = "From: ordersubmission@blahblah.com\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);

 ?>