使用foreach循环的邮件消息在PHP中不起作用

时间:2013-11-27 15:17:41

标签: foreach swiftmailer php

我想邮寄网上商店订单的详细信息,因此需要使用foreach循环来构建我的消息。我认为下面的消息可行,但事实并非如此。我怎样才能做到这一点?

              '<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>'.};

5 个答案:

答案 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>';

答案 1 :(得分: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来显示此表格。

.=的工作方式有点像.,但它会将字符串附加到已包含字符串的变量。

答案 2 :(得分: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>';

答案 3 :(得分:0)

这里:     $ value = $ item ['aantal'] * $ item ['price']; 你应该改变这样:     $ value = $ item [“aantal”] * $ item [“price”]; 因为您的单引号标记为分隔符

答案 4 :(得分: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>';