2013-11-27 49 views
0

我想郵寄網絡商店訂單的詳細信息,因此需要使用foreach循環來構建我的消息。我認爲下面的消息會起作用,但事實並非如此。我該如何做這項工作?帶有foreach循環的郵件在PHP中不起作用

   '<table> 
       <tr> 
        <td class="column-product"><strong>Productomschrijving</strong></td> 
        <td class="column-aantal"><strong>Aantal</strong></td> 
        <td class="column-prijs"><strong>Prijs</strong></td> 
       </tr>'.foreach($shopper_cart as $item) { 
        $value = $item['aantal'] * $item['price']; 
        $sum += $value;.' 
       <tr> 
        <td>'.$item["product_id"].'</td> 
        <td>'.$item["aantal"].'</td> 
        <td>€'.$item["aantal"] * $item["price"].'</td> 
       </tr> 
       </table>'.}; 
+0

「它不工作」從來都不是問題的一個很好的說明。描述你想要你的代碼做什麼以及它做什麼。 –

+3

你不能只把'foreach'放到這樣的字符串中間。這是基本上破壞的語法。它只是不能這樣工作。 – Spudley

+0

我需要在電子郵件中總結訂單。我需要一種方法來做到這一點。 –

回答

4

你不能只把一個foreach放到這樣一個字符串的中間。這是基本上破壞的語法。它只是不能這樣工作。

最適合你的解決方案是申報循環作爲一個單獨的功能,像這樣:

function cartItemTableRows($shopper_cart) { 
    $output = ''; 
    foreach($shopper_cart as $item) { 
     $value = $item['aantal'] * $item['price']; 
     $sum += $value; 
     $output .='<tr> 
        <td>'.$item["product_id"].'</td> 
        <td>'.$item["aantal"].'</td> 
        <td>€'.$item["aantal"] * $item["price"].'</td> 
       </tr>'; 
    } 
    return $output; 
} 

現在你可以把一個呼叫轉移到新功能到現有的代碼,你正在構建的字符串:

$tableHTML = '<table> 
    <tr> 
     <td class="column-product"><strong>Productomschrijving</strong></td> 
     <td class="column-aantal"><strong>Aantal</strong></td> 
     <td class="column-prijs"><strong>Prijs</strong></td> 
    </tr>'.cartItemTableRows($shopper_cart).' 
</table>'; 
0

您只能使用.將字符串連接在一起。你需要沿着這行做一些事情:

  $order_details_table = '<table> 
      <tr> 
       <td class="column-product"><strong>Productomschrijving</strong></td> 
       <td class="column-aantal"><strong>Aantal</strong></td> 
       <td class="column-prijs"><strong>Prijs</strong></td> 
      </tr>'; 

      // append a row to the table string for each order item 
      foreach($shopper_cart as $item) { 
       $value = $item['aantal'] * $item['price']; 
       $sum += $value; 
       $order_details_table .= ' 
       <tr> 
        <td>'.$item["product_id"].'</td> 
        <td>'.$item["aantal"].'</td> 
        <td>€'.$item["aantal"] * $item["price"].'</td> 
       </tr>'; 
      } 

      $order_details_table .= '</table>'; 

然後你可以使用$order_details_table您希望這個表出現。

.=的工作原理與.有點相似,但它將字符串追加到已包含字符串的變量中。

0

你不能像這樣在一個字符串中嵌入一個foreach循環。你需要打破字符串,然後運行你的foreach()。

$emailBody = '<table> 
        <tr> 
         <td class="column-product"><strong>Productomschrijving</strong></td> 
         <td class="column-aantal"><strong>Aantal</strong></td> 
         <td class="column-prijs"><strong>Prijs</strong></td> 
        </tr>'; 
foreach($shopper_cart as $item) { 
     $emailBody .= '<tr> 
         <td>'.$item["product_id"].'</td> 
         <td>'.$item["aantal"].'</td> 
         <td>€'.$item["aantal"] * $item["price"].'</td> 
        </tr>'; 
} 
      $emailBody .= '</table>'; 
0

這裏: $值= $項目[ 'aantal'] * $項目[ '價格']; 你應該改變是這樣的: $ value = $ item [「aantal」] * $ item [「price」]; ,因爲你的單引號作爲分隔符

0

由於@Spudley說你不能連接這樣的foreach。你需要將它從字符串中分離出來。在添加之前,您可能還需要初始化$sum var。

代碼應該是這個樣子:

$table = '<table> 
    <tr> 
     <td class="column-product"><strong>Productomschrijving</strong></td> 
     <td class="column-aantal"><strong>Aantal</strong></td> 
     <td class="column-prijs"><strong>Prijs</strong></td> 
    </tr>'; 

$sum = 0; 
foreach($shopper_cart as $item) { 
    $value = $item['aantal'] * $item['price']; 
    $sum += $value; 
    $table .= '<tr> 
     <td>'.$item["product_id"].'</td> 
     <td>'.$item["aantal"].'</td> 
     <td>€'.$item["aantal"] * $item["price"].'</td> 
    </tr>'; 
} 

$table .= '</table>';