2013-08-17 47 views
0

我需要幫助,我需要郵件通過PHP郵件功能,所以在消息字段中,我應該做一個HTML文本,但在這個文本中我有循環計數數組來回顯表與它的數據,但出現了問題,有沒有人知道爲什麼它不工作?你的代碼的爲什麼我的foreach循環沒有工作?

public function sendmail($arr){ 
    $to = $_SESSION['email']; 
    $subject = 'کارت شارژ'; 
    $message = '<html> 
        <head> 
         <title>کد شارژ خریداری شده شما</title> 
        </head> 
        <body> 
         <p>از خرید شما متشکریم</p> 
         <table> 
         <tr> 
          <th>#شماره</th><th>کد شارژ</th> 
         </tr> 
         '+ 
         foreach ($arr as $i){ 
          echo '<tr><td>'.$i.'</td></tr>'; 
         } 
          +' 
         </table> 
        </body> 
       </html>'; 
    $headers = 'MIME-Version: 1.0' . "\r\n"; 
    $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n"; 

    $headers .= 'To: '.$_SESSION['phone'].' <'.$to.'>' . "\r\n"; 
    $headers .= 'From: hameja123 <[email protected]>' . "\r\n"; 
    mail($to, $subject, $message, $headers); 
} 
+0

你不能在一箇中間扔一個循環串。 – j08691

回答

0

更改部分:

$data = ''; 
foreach ($arr as $i){ 
    $data .= '<tr><td>'.$i.'</td></tr>'; 
} 
$message = '<html> 
       <head> 
        <title>کد شارژ خریداری شده شما</title> 
       </head> 
       <body> 
        <p>از خرید شما متشکریم</p> 
        <table> 
        <tr> 
         <th>#شماره</th><th>کد شارژ</th> 
        </tr> 
        '.$data.' 
        </table> 
       </body> 
      </html>'; 

你不能用字符串插入的foreach內聯。

2

你不能將代碼拼接成像這樣的字符串。此外,+是用於添加,而不是連接。

嘗試預先計算的行:

$rows = ""; 
foreach($arr as $i) $rows .= "<tr><td>".$i."</td></tr>"; 

然後你可以在串連它:

"...... ".$rows." ......."; 
+0

Kolnik感謝它的工作原理,但在另一個問題中,當郵件功能完成時,我的郵箱中沒有郵件,爲什麼?你能幫我一下嗎? –

0

不要使用

foreach ($arr as $i){ 
          echo '<tr><td>'.$i.'</td></tr>'; 
         } 

的 「$消息=」 語句中。

相反,前$message = '<html>

$string=''; 
foreach($arr as $i) 
$string = $string.'<tr><td>$i</td><tr>'; 

做到這一點現在,$消息=裏面,

做到這一點:

<tr> 
          <th>#شماره</th><th>کد شارژ</th> 
         </tr> 
         $string 
         </table>