带有foreach循环的邮件是';t在PHP中工作


Mail message with foreach loop isn't working in 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>'.};

不能像那样把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>';

只能使用.将字符串连接在一起。你需要做一些类似的事情:

          $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

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

不能在这样的字符串中嵌入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>';

此处:$value=$item[‘antal’]*$item[’price’];你应该这样改变:$value=$item["aantal"]*$item["price"];因为您的单引号作为分隔符

正如@Spudley所说,你不能像那样连接foreach。你需要把它和字符串分开。在添加之前,您可能还需要初始化$sum变量。

代码应该是这样的:

$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>';