简体   繁体   English

PHP foreach循环插入PHP变量

[英]PHP foreach loop insert into PHP variable

I need some help constructing the proper structure for an HTML email that I am trying to send using PHP. 我需要一些帮助来构建我尝试使用PHP发送的HTML电子邮件的正确结构。 Here is the line which sends the email: 以下是发送电子邮件的行:

$this->_send_user_email($recipient, $subject, $message);

Where I am having trouble is constructing the $message . 我遇到麻烦的地方是构建$message I need the $message to include a table with a dynamic number of rows. 我需要$message来包含一个动态行数的表。 Here is the current structure of the $message variable: 这是$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>
";  

I am unsure of how to insert my PHP foreach loop inside this existing $messsage variable. 我不确定如何在现有的$messsage变量中插入我的PHP foreach循环。 I get confused about when to close the quotes and how to continue etc. Here is the code for the foreach loop that I am trying to insert: 我对何时关闭引号以及如何继续等感到困惑。以下是我尝试插入的foreach循环的代码:

<?php if (is_array($foo['bar'])):  ?>    
<table>
    <tr>
        <th >1</th>
        <th >2</th>
        <th >3</th>
    </tr>
<?php $row = 01; 
foreach ($foo['bar'] as $key => $value): ?>
<tr>
    <td ><?php echo str_pad($row++, 2, '0', STR_PAD_LEFT); ?></td>
    <td >AAA</td>
    <td >BBB</td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>

So I guess my question is: What is the correct way to insert this PHP foreach loop into the $message variable above? 所以我想我的问题是:将这个PHP foreach循环插入上面的$message变量的正确方法是什么?

A way of doing this is splitting the template message in two strings say $message1 and $message2 where the first contains everything up until the dynamic table and the latter everything that starts after the table. 这样做的一种方法是将模板消息拆分为两个字符串,例如$ message1和$ message2,其中第一个包含所有内容,直到动态表,后者包含在表之后的所有内容。 Then you generate your dynamic table with string concatenation and you concatenate the three parts of the whole message. 然后使用字符串连接生成动态表,并连接整个消息的三个部分。

$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;

Now as someone mentioned in the comments, this code will work but is quite messy, in the sense that you heavily mix php logic and plain html text. 现在正如评论中提到的那样,这段代码可以工作,但是非常混乱,因为你大量混合了php逻辑和普通的html文本。 Ideally you want to separate these two as much as possible. 理想情况下,您希望尽可能地将这两者分开。

You are able to create a static HTML message using php that is sent to your user, however you can NOT include php within your email(if that's what you're trying to do) so that the email is dynamic as it is displayed. 您可以使用发送给您的用户的php创建静态HTML消息,但是您不能在您的电子邮件中包含php(如果这是您正在尝试做的事情),以便电子邮件在显示时是动态的。 So assuming you're doing the former, the code in joe42s answer is spot on. 所以假设你正在做前者,那么joe42s答案中的代码就是现实。 he's written if for you! 他写的是给你的!

if you want a dynamically updated email that changes when people look at it (using php), you could generate a link to be sent in the email with parameters in it that would display a webpage customized for the user. 如果你想要一个动态更新的电子邮件,当人们看到它时会发生变化(使用php),你可以在电子邮件中生成一个链接,其中包含可以显示为用户定制的网页的参数。

            <?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>
            ";  
            ?>

Well, your way may work some way or another but i may advice with more proper way to send an email you can use view as message content, for example : 好吧,你的方式可能会以某种方式工作,但我可以建议用更合适的方式发送电子邮件,你可以使用视图作为消息内容,例如:

$data=$foo;
$message = $this->load->view('some view',$data,true);

and you can deal with it as normal view 你可以像普通视图一样处理它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM