![](/img/trans.png)
[英]how can i email the contents of a shopping cart back to the web moderator
[英]How can I email this shopping cart array?
我不知道如何通過電子郵件發送整個購物車陣列。 我嘗試使用print_r函數,但結果中僅得到一項。
任何幫助將不勝感激。
<?php session_start(); ?>
<?php
if (!isset($_SESSION['cart']) || (count($_SESSION['cart']) == 0)) {
echo '<p>Your shopping cart is empty, so <a href="L14O1_buy.php">get shopping</a>.</p>';
} else {
echo '<table>
<tr>
<th>Product</th>
<th>Cost</th>
<th>Units</th>
<th>Subtotal</th>
</tr>';
$total = 0;
foreach($_SESSION['cart'] as $item) {
echo "<tr>
<td>{$item['item']}</td>
<td>\${$item['unitprice']}</td>
<td>{$item['quantity']}</td>
<td>$".($item['unitprice'] * $item['quantity'])."</td>
</tr>";
$total += ($item['unitprice'] * $item['quantity']);
}
echo '</table>';
echo "<p>Grand total: \$$total</p>";
}
?>
<?php
$to = 'blah@gmail.zzz';
$subject = 'the subject';
$body = print_r($item);
$headers = 'From: blah@gmail.zzz' . "\r\n" .
'Reply-To: blah@gmail.zzz' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $body, $headers);
?>
僅顯示最后一項,因為您的foreach ( $_SESSION['cart'] as $item ) ...
循環使用$item
變量。 $ item在每個循環中重新分配。 在循環結束時,保留最后一個值。
嘗試$body = print_r($_SESSION['cart'], true);
代替。
嘗試這個:
<?php
session_start();
$body = '';
if (!isset($_SESSION['cart']) || (count($_SESSION['cart']) == 0)) {
echo '<p>Your shopping cart is empty, so <a href="L14O1_buy.php">get shopping</a>.</p>';
} else {
echo '<table>
<tr>
<th>Product</th>
<th>Cost</th>
<th>Units</th>
<th>Subtotal</th>
</tr>';
$total = 0;
foreach($_SESSION['cart'] as $item) {
echo "<tr>
<td>{$item['item']}</td>
<td>\${$item['unitprice']}</td>
<td>{$item['quantity']}</td>
<td>$".($item['unitprice'] * $item['quantity'])."</td>
</tr>";
$total += ($item['unitprice'] * $item['quantity']);
$body .= print_r($item, true)."\n";
}
echo '</table>';
echo "<p>Grand total: \$$total</p>";
}
$to = 'blah@gmail.zzz';
$subject = 'the subject';
$body = "<pre>{$body}</pre>";
$headers = 'From: blah@gmail.zzz' . "\r\n" .
'Reply-To: blah@gmail.zzz' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $body, $headers);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.