如何包含包含HTML和;PHP编译器体中的PHP代码?
我刚刚用PHP/SQL构建了一个购物车,当客户下订单时,我想给他发送一封包含订单信息的电子邮件。我遇到的问题是如何将产品列表作为表格包含在电子邮件中。我尝试使用一个如何包含包含HTML和;PHP编译器体中的PHP代码?,php,html,foreach,phpmailer,Php,Html,Foreach,Phpmailer,我刚刚用PHP/SQL构建了一个购物车,当客户下订单时,我想给他发送一封包含订单信息的电子邮件。我遇到的问题是如何将产品列表作为表格包含在电子邮件中。我尝试使用一个foreach循环,该循环包含HTML和PHP,但当我尝试在浏览器中“试驾”模板时,它导致HTTP错误500 下面是我的代码的样子: <?php $body = "<h1>We're glad to see you $firstName!</h1> <br /><br /&g
foreach
循环,该循环包含HTML和PHP,但当我尝试在浏览器中“试驾”模板时,它导致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;
?>
foreach($cartItems作为$item){
$body.=”
“$item[“数量”]”
“€”项目[“小计]”;
}
从foreach中删除echo($cartItems作为$item){
<?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;
?>
$body.=”
“$item[“数量”]”
“€”项目[“小计]”;
}
从foreach中删除echo可以设置变量,然后使用foreach将其添加到同一变量中
<?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;
?>
<?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;
?>
您可以设置变量,然后使用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;
?>
您不会在字符串变量中进行回显。移除回声,它应该会工作。例如,$body.=''$项目[“数量”]。“€”。$项目[“小计”]。"";代码>在字符串变量中不回显。移除回声,它应该会工作。例如,$body.=''$项目[“数量”]。“€”。$项目[“小计”]。"";代码>可能要删除分号。可能要删除分号。感谢您的帮助,但我认为这会使在phpMail中实现分号更加困难。这就是为什么我给了您两个选项。一个,以防您在变量中需要它。。如果需要将其输出到浏览器,则使用另一个。谢谢您的帮助,但我认为这会使在phpMail中实现它更加困难。这就是为什么我给了您两个选项。一个,以防您在变量中需要它。。如果需要输出到浏览器,则为另一个。