我刚刚在PHP / SQL中构建了一个购物车,当客户下订单时,我想给他发送一封包含订单信息的电子邮件。我遇到的问题是如何将产品列表作为表格包含在电子邮件中。我尝试使用包含 HTML和PHP 的foreach
循环,但当我尝试“测试驱动器”时,它会导致 HTTP错误500 ;我浏览器中的模板。
以下是我的代码:
<?php
$body = "<h1>We're glad to see you $firstName!</h1> <br /><br />
<p>
Here is a recap of your next trip:<br /><br />
You arrive on the <strong>$arrival</strong> and leave on the <strong>$departure</strong>.<br /><br />
What you ordered:<br />
</p>
<table>
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>"; //everything fine until here
foreach($cartItems as $item) {
$body .= "<tr><td><a href=\"#\">" . echo $item["name"]; . "</a></td>
<td><div>" . echo $item["qty"]; . "</div></td>
<td><span>" . echo "€".$item["subtotal"]; . "</span></td></tr>";
} //foreach end
$body .= "</tbody>
</table>";
echo $body;
?>
我知道$item["#"]
变量工作正常,因为当我尝试使用echo
循环独立foreach
时,它可以工作。当我尝试以某种方式在echo $item["name"]
变量中的HTML中连接PHP(即$body
)时出现问题。
N.B。此代码是我用来测试它是否适用于我的浏览器的代码。最终,$body
变量将是$mail->MsgHTML($body)
的内容。
感谢您的帮助! :)
PS:这是我在本网站上的第一篇文章,请告诉我是否可以改进我的问题!
答案 0 :(得分:0)
foreach($cartItems as $item) {
$body .= "<tr><td><a href=\"#\">" . $item["name"] . "</a></td>
<td><div>" . $item["qty"] . "</div></td>
<td><span>" . "€".$item["subtotal"] . "</span></td></tr>";
}
从foreach中删除回声
答案 1 :(得分:0)
<?php
$body = "<h1>We're glad to see you $firstName!</h1> <br /><br />
<p>
Here is a recap of your next trip:<br /><br />
You arrive on the <strong>$arrival</strong> and leave on the <strong>$departure</strong>.<br /><br />
What you ordered:<br />
</p>
<table>
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>"; //everything fine until here
foreach($cartItems as $item) {
$body .= "<tr><td><a href=''>" .$item["name"]. "</a></td>
<td><div>" .$item["qty"]. "</div></td>
<td><span>€".$item["subtotal"]. "</span></td></tr>";
} //foreach end
$body .= "</tbody>
</table>";
echo $body;
?>
答案 2 :(得分:0)
您可以设置变量,然后使用foreach
添加到同一个变量<?php <?php
$body = "<h1>We're glad to see you $firstName!</h1> <br /><br />
<p>
Here is a recap of your next trip:<br /><br />
You arrive on the <strong>$arrival</strong> and leave on the <strong>$departure</strong>.<br /><br />
What you ordered:<br />
</p>
<table>
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>
";
foreach($cartItems as $item) {
$body .= "<tr><td><a href=\"#\">" . $item["name"] . "</a></td>
<td><div>" . $item["qty"] . "</div></td>
<td><span>€". $item["subtotal"] . "</span></td></tr>";
}
$body .= "</tbody>";
$body .= "</table>";
echo $body;
?>
使用HTML并在PHP中只使用foreach更清楚。
<h1>We're glad to see you $firstName!</h1><br /><br />
<p>
Here is a recap of your next trip:<br /><br />
You arrive on the <strong>$arrival</strong> and leave on the <strong>$departure</strong>.<br /><br />
What you ordered:<br />
</p>
<table>
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>";
<?php foreach($cartItems as $item): ?>
<tr>
<td><a href="#"><?= $item["name"]; ?></a></td>
<td><div><?= $item["qty"]; ?></div></td>
<td><span>€ <?= $item["subtotal"]; ?></span></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>