简体   繁体   English

在MSG标记内插入php代码

[英]Insert php code inside a MSG tag

I have this code which sends an e-mail with some information in it. 我有这段代码可以发送一封包含一些信息的电子邮件。 The message part is like below: 消息部分如下所示:

    $mail->Body    =<<<MSG
            <html>
                <head>
                    <title>Statistics</title>
                </head>
                <body>
                    The start was: {$start} </br></br>
                    The end was: {$end} </br></br>            
                </body>
            </html>
    MSG;

I have an array named $events which I want to include in the message, but I want to display it as a table, as below 我有一个名为$ events的数组,想要包含在消息中,但是我想将其显示为表格,如下所示

$mail->Body    =<<<MSG
        <html>
            <head>
                <title>Statistics</title>
            </head>
            <body>
                The start was: {$start} </br></br>
                The end was: {$end} </br></br>   

                foreach ($events as $event)
                { 
                    echo '<tr>';
                    echo '<td>' . $event)[1] . '</td>';
                    echo '<td>' . $event)[2] . '</td>';
                    echo '<td>' . $event)[3] . '</td>';
                    echo '</tr>';
                }

            </body>
        </html>
MSG;

Can you please help me with the syntax of foreach? 您能帮我用foreach的语法吗?

Prepare your data before heredocs: 在heredocs之前准备数据:

$tableData = '';

foreach ($events as $event) { 
    $tableData .= '<tr>';

    foreach ($event as $cell) {
        $tableData .= "<td>{$cell}</td>";
    }

    $tableData .= '</tr>';
}

$mail->Body = <<<MSG
        <html>
            <head>
                <title>Statistics</title>
            </head>
            <body>
                The start was: {$start} </br></br>
                The end was: {$end} </br></br>

                <table>{$tableData}</table>
            </body>
        </html>
MSG;

You want to display events as a table in an email. 您想要将事件显示为电子邮件中的表格。 Well your foreach in itself is correct but what you do inside has some problems. 好吧,您的学习本身就是正确的,但是您在内部所做的事情存在一些问题。 The most simple solution to your question would be as follows (I never used the technique you are using... Im quite sure that the foreach can not be executed this way): 您问题的最简单解决方案如下(我从未使用过您正在使用的技术...我很确定,不能以这种方式执行foreach):

$mail->Body    =<<<MSG
    <html>
        <head>
            <title>Statistics</title>
        </head>
        <body>
            The start was: {$start} </br></br>
            The end was: {$end} </br></br>   
            <table>
            foreach ($events as $event)
            { 
                echo '<tr>';
                echo '<td>' . $event . '</td>';
                echo '</tr>';
            }
            </table>

        </body>
    </html>
MSG;

As stated in comments it would be better to use normal strings: 如评论中所述,最好使用普通字符串:

$body = '
<html>
    <head>
        <title>Statistics</title>
    </head>
    <body>
        The start was: '.$start.' </br></br>
        The end was: '.$end.' </br></br>   
        <table>';
        foreach ($events as $event)
        { 
            $body .= '<tr><td>' . $event . '</td></tr>';
        }
        $body .= '</table>
    </body>
</html>';
$mail->Body = $body;

I assumed your code segment echo '<td>' . $event)[1] . '</td>'; echo '<td>' . $event)[2] . '</td>'; echo '<td>' . $event)[3] . '</td>'; 我假设您的代码段为echo '<td>' . $event)[1] . '</td>'; echo '<td>' . $event)[2] . '</td>'; echo '<td>' . $event)[3] . '</td>'; echo '<td>' . $event)[1] . '</td>'; echo '<td>' . $event)[2] . '</td>'; echo '<td>' . $event)[3] . '</td>'; to be your first try to handle an array. 是您第一次尝试处理数组。 if $events is multi-dimensional (array of array) you would have to replace 如果$events是多维的(数组数组),则必须替换

$body .= '<tr><td>' . $event . '</td></tr>';

with

$body .= '<tr><td>' . $event[1] . '</td><td>' . $event[1] . '</td><td>' . $event[1] . '</td></tr>';

remember that array indices start with 0 请记住,数组索引以0开头

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

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