[英]Echo An Array inside a EOF statement
I'm trying to send an HTML email receipt from a shopping cart purchase but I don't get how to echo the session array within the EOF area. 我正在尝试从购物车购买中发送HTML电子邮件收据,但我不知道如何在EOF区域内回显会话数组。 php doesn't seem to want to execute here. php似乎不想在这里执行。 Thoughts? 有什么想法吗?
//begin of HTML message
$message = <<<EOF
<html>
<body style="font-family: 'Myriad Pro', 'DejaVu Sans Condensed', Helvetica, Arial, sans-serif;
font-size:10px;"><center>
<table width="750" cellpadding="0" cellspacing="0" class="view-cart" style="text-
align:center;padding:5px;"><tr>
<tr><td>Image</td><td>SKU</td><td>Description</td><td>QTY</td><td>Price</td><td> </td></tr>
<tr>
foreach ($_SESSION["products"] as $cart_itm)
{
echo '<td><img src="'.$cart_itm['image'].'"></td>';
echo '<td>';
echo '<div class="p-code">'.$cart_itm["code"].'</td>';
echo '<td align="left">'.$cart_itm["description"].'</td>';
echo '<td>'.$cart_itm["qty"].'</td>';
echo '<td><div class="p-price">'.$cart_itm["price"].' each</div></td>';
echo '<td></td>';
echo '</tr>';
}
</table>
</center>
</body>
</html>
EOF;
//end of message
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $message, $headers);
The updated code now looks like this. 现在,更新后的代码如下所示。 I replaced the "$message +=" with "$message .=" and it seems to work fine now. 我用“ $ message。=”替换了“ $ message + =”,现在看来工作正常。 : :
//begin of HTML message
$message = <<<EOF
<html>
<body style="font-family: 'Myriad Pro', 'DejaVu Sans Condensed', Helvetica, Arial, sans-serif; font-size:10px;"><center>
<table width="750" cellpadding="0" cellspacing="0" class="view-cart" style="text-align:center;padding:5px;">
<tr><td>Image</td><td>SKU</td><td>Description</td><td>QTY</td><td>Price</td><td> </td></tr>
<tr>
EOF;
foreach ($_SESSION["products"] as $cart_itm)
{
$message .= "<td><img src=".$cart_itm['image'].">";
$message .= "</td><td>".$cart_itm['code'];
$message .= "</td><td>".$cart_itm['description'];
$message .= "</td><td>".$cart_itm['qty'];
$message .= "</td><td>".$cart_itm['price'] ;
$message .= "each</td><td></td></tr>";
}
$message2 = <<<EOF
</table>
<hr><table width="750" cellpadding="0" cellspacing="0" class="view-cart"><tr>
<td align="right"><b>Shipping: $ $shipping </b><br><b>Total: $ $total </b></td></tr></table>
</center>
</body>
</html>
EOF;
What you're doing is creating a string using the heredoc syntax ; 您正在执行的操作是使用Heredoc语法创建字符串; so everything that's after the first newline and before the identifier ( EOF
in your case) is considered part of that string, and no code will be executed inside of it besides variables and escape sequences for special characters. 因此,在第一个换行符之后和标识符(在您的情况下为EOF
)之前的所有内容都被视为该字符串的一部分,除了变量和特殊字符的转义序列外,该代码内部不会执行任何代码。
As far as I know there is no solution besides ending the string just before the loop, then replacing all instances of echo
with $message +=
in the loop and finally creating a new string (using heredoc or conventional quotes and escaped newlines) after the loop and appending that new string to the $message
. 据我所知,除了在循环之前结束字符串,然后在循环中将所有echo
实例替换为$message +=
并最终在循环并将新字符串附加到$message
。
Another (bad in my opinion) approach would be to put a placeholder for the loop's contents in the heredoc, then creating a new string that will hold the output of the loop and finally replacing the placeholder with that string. 另一种(我认为不好)的方法是在heredoc中放置循环内容的占位符,然后创建一个新字符串以保存循环的输出,最后用该字符串替换占位符。
Edit: for your updated code, note that the identifier EOF1
should be at the beginning of a new line and be terminated with a semicolon. 编辑:对于更新的代码,请注意,标识符EOF1
应该在新行的开头,并以分号终止。
Also, the identifiers aren't supposed to be unique, it's fine to use the same identifier for different strings, no need for EOF1
, 2, etc. 另外,标识符也不应该唯一,对于不同的字符串可以使用相同的标识符,不需要EOF1
等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.