Reputation: 83
I just built a shopping cart in PHP/SQL and when the client places an order I want to send him an email with his order info. The issues I am having is how to include the product list within the email as a table. I tried to use a foreach
loop containing both HTML and PHP but it resulted in an HTTP error 500 when I tried to 'test drive' the template in my browser.
Here is what my code looks like :
<?php
$body = "<h1>We're glad to see you $firstName!</h1> <br /><br />
<p>
Here is a recap of your next trip:<br /><br />
You arrive on the <strong>$arrival</strong> and leave on the <strong>$departure</strong>.<br /><br />
What you ordered:<br />
</p>
<table>
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>"; //everything fine until here
foreach($cartItems as $item) {
$body .= "<tr><td><a href=\"#\">" . echo $item["name"]; . "</a></td>
<td><div>" . echo $item["qty"]; . "</div></td>
<td><span>" . echo "€".$item["subtotal"]; . "</span></td></tr>";
} //foreach end
$body .= "</tbody>
</table>";
echo $body;
?>
I know the $item["#"]
variables are working fine because when I try to echo
them independently with a foreach
loop it works. The problem comes when I try to somehow concatenate PHP (i.e. echo $item["name"]
) within HTML within the $body
variable.
N.B. This code is the one I use to test if it works on my browser. Ultimately the $body
variable will be the content of $mail->MsgHTML($body)
.
Thank you for your help! :)
PS: this is my first post on this website, please tell me if I can improve my question!
Upvotes: 1
Views: 1074
Reputation: 46
<?php
$body = "<h1>We're glad to see you $firstName!</h1> <br /><br />
<p>
Here is a recap of your next trip:<br /><br />
You arrive on the <strong>$arrival</strong> and leave on the <strong>$departure</strong>.<br /><br />
What you ordered:<br />
</p>
<table>
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>"; //everything fine until here
foreach($cartItems as $item) {
$body .= "<tr><td><a href=''>" .$item["name"]. "</a></td>
<td><div>" .$item["qty"]. "</div></td>
<td><span>€".$item["subtotal"]. "</span></td></tr>";
} //foreach end
$body .= "</tbody>
</table>";
echo $body;
?>
Upvotes: 0
Reputation: 9143
You can set the variable and then add to that same variable using the foreach
<?php <?php
$body = "<h1>We're glad to see you $firstName!</h1> <br /><br />
<p>
Here is a recap of your next trip:<br /><br />
You arrive on the <strong>$arrival</strong> and leave on the <strong>$departure</strong>.<br /><br />
What you ordered:<br />
</p>
<table>
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>
";
foreach($cartItems as $item) {
$body .= "<tr><td><a href=\"#\">" . $item["name"] . "</a></td>
<td><div>" . $item["qty"] . "</div></td>
<td><span>€". $item["subtotal"] . "</span></td></tr>";
}
$body .= "</tbody>";
$body .= "</table>";
echo $body;
?>
It is clearer to just use HTML and to do only the foreach in PHP.
<h1>We're glad to see you $firstName!</h1><br /><br />
<p>
Here is a recap of your next trip:<br /><br />
You arrive on the <strong>$arrival</strong> and leave on the <strong>$departure</strong>.<br /><br />
What you ordered:<br />
</p>
<table>
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>";
<?php foreach($cartItems as $item): ?>
<tr>
<td><a href="#"><?= $item["name"]; ?></a></td>
<td><div><?= $item["qty"]; ?></div></td>
<td><span>€ <?= $item["subtotal"]; ?></span></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
Upvotes: 1
Reputation:
foreach($cartItems as $item) {
$body .= "<tr><td><a href=\"#\">" . $item["name"] . "</a></td>
<td><div>" . $item["qty"] . "</div></td>
<td><span>" . "€".$item["subtotal"] . "</span></td></tr>";
}
remove echo from foreach
Upvotes: 0