0

I have this code that write inside the variable $products a string of products name:

$products = '';
foreach($order->pad_products as $product) $products .= " $product->title";

When I print $products inside a HTML email body and send the email with PHP, in this case, I sand $msg, I see the name of the products all in one line.

$msg = "
  <html>
    <head>
      <style type='text/css'></style>
    </head>
    <body>
      <h1>Sent!</h1>
      <p>$name thank you</p>
        $products
    </body>
  </html>";

How can I make a list of these products? and, can I delete one of these?

4
  • 1
    As you use html markup - add <br /> or create a <ul> for example. Commented Sep 5, 2018 at 8:38
  • Possible duplicate of How create responsive email that send from php? Commented Sep 5, 2018 at 8:38
  • @RonnieOosting no, it's about how write php variable. I not about how send email. Commented Sep 5, 2018 at 8:46
  • @u_mulder thank you for your help, but in this particular situation I can't write in html I have to use the variable in php. good solution it's the one below. Commented Sep 5, 2018 at 8:47

2 Answers 2

2

To display them as a list, you simply have to create a list using HTML:

<?php
echo "<ul>";
foreach($order->pad_products as $product) {
    echo "<li>" . $product->title . "</li>";
}
echo "</ul>";
?>

Alternative, applying to your current code

<?php
$products = "";
foreach($order->pad_products as $product) {
    $products .= "<li>".$product->title."</li>";
}
$msg = "
<html>
<body>
  <h1>Sent!</h1>
    $products
</body>
</html>";
Sign up to request clarification or add additional context in comments.

2 Comments

wee, thank you! this work! was easy I didn't try this possibility! thank you!! Do you know How I can delete the last list row?
@MarcoRomano take a look at this stackoverflow.com/questions/21899944/…
1

ProcessWire (which you tagged this with) lets you do this with pop():

<?php
echo "<ul>";
$products = $order->pad_products;
$products->pop(); 
foreach($products as $product) {
    echo "<li>" . $product->title . "</li>";
}
echo "</ul>";
?>

Another option is to count the number of items and limit to one less than that. You should be able to use ->count() but I don't know what pad_products is, so if that doesn't work, you could just do count($order->pad_products)

<?php
echo "<ul>";
$limit = $order->pad_products->count() - 1;
foreach($order->pad_products("limit=$limit") as $product) {
    echo "<li>" . $product->title . "</li>";
}
echo "</ul>";
?>

2 Comments

No problem - I added another option for you as well. There are many different ways to achieve this :)
nice, I will try to use. At moment for delete the Shipping Cost row I have use this: $products = ''; foreach($order->pad_products as $product) $products .= "<li>".$product->title."</li>"; $words = explode( " ", $products ); array_splice( $words, -2 ); $prod = implode( " ", $words ); echo $prod;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.