Question

I want to mail the details of a web shop order and therefore need to use a foreach loop to construct my message. I thought the message below would work, but it doesn't. How can I make this work?

              '<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>'.};
Was it helpful?

Solution

You can't just put a foreach into the middle of a string like that. That's fundamentally broken syntax. It just doesn't work like that.

The best solution for you is to declare the loop as a separate function, like so:

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

Now you can put a call to your new function into the existing code where you're building the string:

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

OTHER TIPS

You can only use . to join strings together. You'd need to do something along the lines of this:

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

Then you can use $order_details_table where you want this table to appear.

The .= works a bit like ., but it appends a string to a variable already containing a string.

You can't embed a foreach loop in a string like that. You need to break out of the string, then run your 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>';

here: $value = $item['aantal'] * $item['price']; you should be Changed like this: $value = $item["aantal"] * $item["price"]; because your Single quotation marks as delimiters

As @Spudley says you can't concatenate a foreach like that. You need to separate it from the string. You may also want to initialize the $sum var before adding to it.

The code should look something like this:

$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>';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top