PHP foreach循环插入到PHP变量中
我需要一些帮助,为我试图使用PHP发送的HTML电子邮件构造适当的结构。以下是发送电子邮件的线路:PHP foreach循环插入到PHP变量中,php,html,codeigniter,email,html-email,Php,Html,Codeigniter,Email,Html Email,我需要一些帮助,为我试图使用PHP发送的HTML电子邮件构造适当的结构。以下是发送电子邮件的线路: $this->_send_user_email($recipient, $subject, $message); 我遇到的问题是构造$message。我需要$message包含一个具有动态行数的表。以下是$message变量的当前结构: $message = "Hello, <br><br> Please review details in the table b
$this->_send_user_email($recipient, $subject, $message);
我遇到的问题是构造$message
。我需要$message
包含一个具有动态行数的表。以下是$message
变量的当前结构:
$message = "Hello, <br><br>
Please review details in the table below:
<br><br>
**** NEED TO INSERT DYNAMIC PHP TABLE HERE ****
<br><br>
If you have questions, please call 1-888-888-8888 and we will assist you.
<br><br>
Customer Support<br>
<br><br>
";
所以我想我的问题是:将这个PHP foreach循环插入上面的$message
变量的正确方法是什么?
<?php
$message = "Hello, <br><br>
Please review details in the table below:
<br><br>";
if (is_array($foo['bar'])) {
$message .= "<table>
<tr>
<th >1</th>
<th >2</th>
<th >3</th>
</tr>";
$row = 01;
foreach ($foo['bar'] as $key => $value) {
$message .= "<tr>
<td >".str_pad($row++, 2, '0', STR_PAD_LEFT)."</td>
<td >AAA</td>
<td >BBB</td>
</tr>";
}
$message .= "</table>";
}
$message .= "<br><br>
If you have any questions, please call 1-888-888-8888 and we will be happy to assist you.
<br><br>
Customer Support<br>
<br><br>
";
?>
您可以使用发送给用户的php创建静态HTML消息,但是您不能在电子邮件中包含php(如果您正试图这样做的话),因此电子邮件在显示时是动态的。所以假设你在做前一个,joe42s答案中的代码是正确的。他是为你写的
如果您想要一封动态更新的电子邮件,当人们看到它时它会发生变化(使用php),您可以生成一个要在电子邮件中发送的链接,其中包含参数,该参数将显示为用户定制的网页。一种方法是将模板消息拆分为两个字符串,例如$message1和$message2,其中第一个字符串包含动态表之前的所有内容,第二个字符串包含表之后开始的所有内容。然后使用字符串连接生成动态表,并连接整个消息的三个部分
$message1 = "Hello, <br><br>
Please review details in the table below:
<br><br>";
$message2 = "<br><br>
If you have questions, please call 1-888-888-8888 and we will assist you.
<br><br>
Customer Support<br>
<br><br>";
$row = "01";
$table = "";
if(is_array($foo['bar'])) {
$table .= "<table>
<tr>
<th >1</th>
<th >2</th>
<th >3</th>
</tr>";
foreach($foo['bar'] as $key => $value) {
$table .= "<tr>
<td >" . str_pad($row++, 2, '0', STR_PAD_LEFT) . "</td>
<td >AAA</td>
<td >BBB</td>
</tr>";
}
$table .= "</table>";
}
$message = $message1 . $table. $message2;
$message1=“你好,
请查看下表中的详细信息:
“;
$message2=“
如果您有任何疑问,请致电1-888-888-8888,我们将为您提供帮助。
客户支持
“;
$row=“01”;
$table=“”;
如果(是_数组($foo['bar']){
$table.=”
1.
2.
3.
";
foreach($foo['bar']作为$key=>$value){
$table.=”
.str_pad($row++,2,'0',str_pad_LEFT)
AAA
BBB
";
}
$table.=”;
}
$message=$message1$桌子$信息2;
现在,正如有人在评论中提到的,这段代码可以工作,但相当混乱,因为您大量混合了php逻辑和纯html文本。理想情况下,你应该尽可能地将这两种方式分开。好吧,你的方式可能会以某种方式起作用,但我可能会建议用更合适的方式发送电子邮件 您可以将视图用作消息内容,例如:
$data=$foo;
$message = $this->load->view('some view',$data,true);
您可以像普通视图一样处理它谢谢您的帮助性解释。我回家后要试试这个。非常感谢。您所说的“使用字符串连接生成动态表”是什么意思?谢谢joe42,这看起来很棒。当我回到家的时候,我将尝试一下。除了遵循给出的答案中的一些建议之外,你应该真正尝试将你的标记(HTML)和你的PHP完全分开!您应该将
$foo
作为$data
的数组元素进行传递,如下所示:$data=array('foo'=>$foo)
否则,您将为$foo
中的每个索引创建一个新变量,而不是仅在视图文件中创建一个名为$foo
的变量。
$data=$foo;
$message = $this->load->view('some view',$data,true);