2014-02-20 127 views
0

我想在PHP Mailer上的msgHTML中運行一個foreach循環,以使數組中的表td動態化。以下是我擁有的最新代碼。我在Dreamweaver上工作,它顯示這個代碼有錯誤,我不知道如何解決這個問題。在PHPMailer的msgHTML中運行foreach循環

<?php 
$mail->Subject = "Vacancy No: " . $vacancy_no . " - " . $vacancy_title; 
$mail->MsgHTML(
" 
Some html and text goes here.... 
Below is the main part of this topic. 

<u><strong>Referees & Contact</strong></u><br/> 
<table width='100%' border='0' cellpadding='0' cellspacing='0' style='border: 1px solid #E2E2E2;'> 
    <tr style='background: #003e82; color: #FFF; font-weight: bold;'> 
     <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Name</td> 
     <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Position & Organization</td> 
     <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Phone Contact</td> 
     <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Email</td> 
    </tr> 
" 
?> 
    <?php 
     foreach($variable as $var){ 
    ?> 
     <tr> 
      <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Name</td> 
      <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Position & Organization</td> 
      <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Phone Contact</td> 
      <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Email</td> 
     </tr> 
    <?php 
     } 
    ?> 
    <?php " 
</table> 
"); 
?> 
+0

在你不輸出什麼'$ var'具有環路,你總是使頭 – 2014-02-20 23:47:41

回答

1

這段代碼如何?

$mail->Subject = "Vacancy No: " . $vacancy_no . " - " . $vacancy_title; 

$msg = "Some html and text goes here.... 
    Below is the main part of this topic. 

    <u><strong>Referees & Contact</strong></u><br/> 
    <table width='100%' border='0' cellpadding='0' cellspacing='0' style='border: 1px solid #E2E2E2;'> 
    <tr style='background: #003e82; color: #FFF; font-weight: bold;'> 
     <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Name</td> 
     <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Position & Organization</td> 
     <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Phone Contact</td> 
     <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Email</td> 
    </tr>"; 

foreach($variable as $var){ 

    $msg .= "<tr> 
      <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Name</td> 
      <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Position & Organization</td> 
      <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Phone Contact</td> 
      <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Email</td> 
     </tr>"; 

} // foreach 

$msg .= "</table>"; 

$mail->MsgHTML($msg); 
1

試試這個

$mail->Subject = "Vacancy No: " . $vacancy_no . " - " . $vacancy_title; 


     $message = 
       " 
       Some html and text goes here.... 
       Below is the main part of this topic. 

       <u><strong>Referees & Contact</strong></u><br/> 
       <table width='100%' border='0' cellpadding='0' cellspacing='0' style='border: 1px solid #E2E2E2;'> 
        <tr style='background: #003e82; color: #FFF; font-weight: bold;'> 
         <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Name</td> 
         <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Position & Organization</td> 
         <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Phone Contact</td> 
         <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Email</td> 
        </tr> 
       "; 

     foreach ($variable as $var) { 

      $message .= "<tr> 
          <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Name</td> 
          <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Position & Organization</td> 
          <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Phone Contact</td> 
          <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Email</td> 
         </tr>"; 
     } 


     $message .= " 
       </table> 
       "; 

     $mail->MsgHTML($message);