doutuo3935 2017-09-22 08:04
浏览 28
已采纳

在MSG标记内插入php代码

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

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

  • 写回答

2条回答 默认 最新

  • droxlzcgnr639823 2017-09-22 08:14
    关注

    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):

    $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>'; to be your first try to handle an array. if $eventsis multi-dimensional (array of array) you would have to replace

    $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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀