协助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

"." 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>...'

in php you can combine assignment operators (http://php.net/manual/en/language.operators.assignment.php)

in this case you can combine . and = to make .=

when you are looping your foreach change

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

to

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

this will add the string on each new loop to your string.

Use like this:

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