dsegw3424 2015-12-02 17:44
浏览 5

协助PHP和HTML电子邮件语法

I think its plainly obvious what Im trying to do here.

$message is the message param from wp_mail().

I need this foreach loop to run inside my email to get all the dynamic values from a form.

   $tmp = '';
    $i = 0;      
        foreach( $_POST as $key => $value)  {
            if( substr( $key, 0, 14) == 'course-select-')   {
                $tmp = '<tr><td>Day number' . $i . ' :</td><td> ' . $value . '</td></tr>';
            }
        $i++;
    };


   //construct the email
    $message = '
    <html>
    <head>
      <title>Booking Confirmation Part 2</title>
    </head>
    <body>
      <h2>Booking Confirmation Part 2 : ' . $golfersname . '</h2>
      <table width="500">
        <tr>
          <td>Accomodation</td>
          <td>' . $accomodation . '</td>
        </tr>
        <tr>
          <td>Singles</td>
          <td>' . $singles . '</td>
        </tr>
        <tr>
          <td>Doubles</td>
          <td>' . $doubles . '</td>
        </tr>
        <tr>
          <td>Car Type</td>
          <td>' . $cartype . '</td>
        </tr>   
        <tr>
          <td>Course Details</td>
        </tr>       
        ' . $tmp . '
      </table>
    </body>
    </html>
    '; 

Everything breaks from the moment it hits the '. $i = 0; . What real stupid thing am I missing out on?

The foreach does work as I’ve tested it already in my console.

Thanks

  • 写回答

3条回答 默认 最新

  • douguanyan9928 2015-12-02 17:52
    关注

    "." are used to build STRINGS in PHP. It seems you are trying to execute code (i.e. the loop and the assignment to $i) halfway through your string. This does not make sense at all.

    The best way to do this would be:

    $message = "<HTML>....Course details";
    $i = 0;     
    foreach( $_POST as $key => $value)
    {
      if( substr( $key, 0, 14) == 'course-select-')
      {
           $message=$message.'Day number' . $i . ' : ' . $value;
      }
      $i++;
    }
    $message=$message."</TD>...</HTML>";
    

    Then complete your email script.

    EDIT

    Yes you have now addressed the first issue. The reason it is only showing the first result is because you are reassigning $tmp in your loop. See below:

    $tmp = '<tr><td>...'
    

    should be

    $tmp = $tmp.'<tr><td>...'
    
    评论

报告相同问题?

悬赏问题

  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。