2012-02-04 60 views
0

我有一個對象數組並希望創建一個在郵件上發送的在線發票。爲此,我修改了以下消息:當爲郵件格式化時,Foreach循環無法在PHP中工作()

function mail_invoice($buyer_info,$items_info ,$date,$final_amount) 
{ 
    global $template; 

    //var_dump($buyer_info); 


    $to = $buyer_info->email; 
    $from_mail = '[email protected]'; 
    //echo $msg.$to.$from;sender_mail 
    $subject = "Your Invoice"; 
    $message = "Your Shopping Details"; 
    $message .= ' 
      <h2>RETAIL INVOICE</h2> 
       <p> 
        SRI SAI VENTURES PVT LTD.<br /> 
        House Number 13A<br /> 
        Nizarapar<br /> 
        Guwahati - 781003<br /> 
        Assam 
       </p> 

       <p> 
        Buyer <br /> 
        <b>'.$buyer_info->fname.' '.$buyer_info->lname.'</b><br /> 
        '.$buyer_info->address.'<br /> 
        PIN - '.$buyer_info->pin.'<br /> 
        PHONE '.$buyer_info->phone.'<br /> 
       </p> 

       <p> 
        Dispatch Date : '.$date.' 
       </p> 
       <table> 
        <tr> 
         <th>Sl No</th> 
         <th>Description of goods</th> 
         <th>Quantity</th> 
         <th>Rate</th> 
         <th>Amount</th> 
        </tr>'. 

       (foreach $items_info as $a=>$v).'  <----line no 1025 
        <tr> 
         <td>'.++$ind.'</td> 
         <td>'.$v->brand.','.$v->model.'</td> 
         <td>'.$v->quantity.'</td> 
         <td>'.$v->price.'</td> 
         <td>'.$v->total_cost.'</td> 
        </tr>'. 
       {/foreach} 
        .'<tr> 
         <td>&nbsp;</td> 
         <td>&nbsp;</td> 
         <td>&nbsp;</td> 
         <td>Total Cost </td> 
         <td>'.$final_amount.'</td> 
        </tr> 
       </table> 





    '; 
    $from = $from_mail; 
    //$headers = "From:" . $from; 

    $headers = "From:" . $from . "\r\n" . 
       "Reply-To:" . $from . "\r\n" . 
       "X-Mailer: PHP/" . phpversion(); 
    $headers .= 'MIME-Version: 1.0' . "\r\n"; 
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";   


    mail($to,$subject,$message,$headers); 

    //$template->main_content = $template->fetch(TEMPLATE_DIR . 'messag_sent_to_friend.html');  
} 

但是我收到錯誤消息Parse error: syntax error, unexpected T_FOREACH in C:\wamp\www\eshop\class\base\user.base.class.php on line 1025。我在做什麼錯?或者我不能在那裏使用foreach。請幫助

回答

2

您不能在字符串連接中嵌套foreach語句。只需把它分解:

.... 
        <th>Amount</th> 
       </tr>'; 

    foreach $items_info as $a=>$v){ 
     $message .= '<tr> 
        <td>'.++$ind.'</td>   
        <td>'.$v->brand.','.$v->model.'</td> 
        <td>'.$v->quantity.'</td> 
        <td>'.$v->price.'</td> 
        <td>'.$v->total_cost.'</td> 
       </tr>'; 
    } 
    $message .= '<tr> 
        <td>&nbsp;</td> 
        <td>&nbsp;</td> 
    .... 
+0

謝謝Wrikken ...我已經分手了,現在工作了 – AssamGuy 2012-02-04 10:50:21

1
foreach ($items_info as $a=>$v){ 

$message .= ' <tr> <td>........ 
+0

我修飾以'的foreach($ items_info爲$ a => $ v)的{「。 \t \t \t \t \t \t \t \t \t \t \t​​'$ + IND。' \t \t \t \t \t \t​​'$ V->品牌 ''。$ V->模式'。 \t \t \t \t \t \t​​'$ V->的數量。' \t \t \t \t \t \t​​'$ V->價格'。 \t \t \t \t \t \t​​'$ V-> TOTAL_COST。' \t \t \t \t \t'。 \t \t \t \t}'但是相同的結果 – AssamGuy 2012-02-04 10:45:24

+0

查看更新的代碼 – mgraph 2012-02-04 10:49:50

0
function mail_invoice($buyer_info,$items_info ,$date,$final_amount) 

{ 全球$模板;

//var_dump($buyer_info); 


$to = $buyer_info->email; 
$from_mail = '[email protected]'; 
//echo $msg.$to.$from;sender_mail 
$subject = "Your Invoice"; 
$message = "Your Shopping Details"; 
$message .= ' 
     <h2>RETAIL INVOICE</h2> 
      <p> 
       SRI SAI VENTURES PVT LTD.<br /> 
       House Number 13A<br /> 
       Nizarapar<br /> 
       Guwahati - 781003<br /> 
       Assam 
      </p> 

      <p> 
       Buyer <br /> 
       <b>'.$buyer_info->fname.' '.$buyer_info->lname.'</b><br /> 
       '.$buyer_info->address.'<br /> 
       PIN - '.$buyer_info->pin.'<br /> 
       PHONE '.$buyer_info->phone.'<br /> 
      </p> 

      <p> 
       Dispatch Date : '.$date.' 
      </p> 
      <table> 
       <tr> 
        <th>Sl No</th> 
        <th>Description of goods</th> 
        <th>Quantity</th> 
        <th>Rate</th> 
        <th>Amount</th> 
       </tr>'; 

      foreach ($items_info as $v){ 
       $message .= '<tr> 
        <td>'.++$ind.'</td> 
        <td>'.$v->brand.','.$v->model.'</td> 
        <td>'.$v->quantity.'</td> 
        <td>'.$v->price.'</td> 
        <td>'.$v->total_cost.'</td> 
       </tr>'; 
      } 
       $message .= '<tr> 
        <td>&nbsp;</td> 
        <td>&nbsp;</td> 
        <td>&nbsp;</td> 
        <td>Total Cost </td> 
        <td>'.$final_amount.'</td> 
       </tr> 
      </table>'; 
$from = $from_mail; 
//$headers = "From:" . $from; 

$headers = "From:" . $from . "\r\n" . 
      "Reply-To:" . $from . "\r\n" . 
      "X-Mailer: PHP/" . phpversion(); 
$headers .= 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";   


mail($to,$subject,$message,$headers); 

//$template->main_content = $template->fetch(TEMPLATE_DIR . 'messag_sent_to_friend.html');  

}