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?

Manav
  • 553
  • 7
  • 18
Marco Romano
  • 272
  • 1
  • 14
  • 1
    As you use `html` markup - add `
    ` or create a `
      ` for example.
    – u_mulder Sep 05 '18 at 08:38
  • Possible duplicate of [How create responsive email that send from php?](https://stackoverflow.com/questions/51944973/how-create-responsive-email-that-send-from-php) – Ronnie Oosting Sep 05 '18 at 08:38
  • @RonnieOosting no, it's about how write php variable. I not about how send email. – Marco Romano Sep 05 '18 at 08: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. – Marco Romano Sep 05 '18 at 08:47

2 Answers2

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>";
Pieter De Clercq
  • 1,951
  • 1
  • 17
  • 29
  • 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? – Marco Romano Sep 05 '18 at 08:45
  • 1
    @MarcoRomano take a look at this https://stackoverflow.com/questions/21899944/php-how-to-skip-last-element-in-foreach-loop – Pieter De Clercq Sep 05 '18 at 08:47
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>";
?>
adrian
  • 111
  • 3
  • 11
  • No problem - I added another option for you as well. There are many different ways to achieve this :) – adrian Sep 06 '18 at 13:42
  • 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 .= "
  • ".$product->title."
  • "; $words = explode( " ", $products ); array_splice( $words, -2 ); $prod = implode( " ", $words ); echo $prod;` – Marco Romano Sep 06 '18 at 15:30