-1

I am building add to card codes with php mailer.

I am creating send shopping cart items with email. There are in mail body five foreach array, i need create one table in there but table rows can not correct, how can I fix it?

    $mail->Body = "<div class='container-fullwidth'>
  <h2>Sipariş Özeti</h2>
  <hr>
<div class='table-responsive'>          
  <table width='100%' class='table table-striped'>
    <thead>
      <tr>
        <th><h2>Ürün</h2></th>
        <th><h2>Gramaj</h2></th>
        <th><h2>Adet</h2></th>
        <th><h2>Birim Fiyat</h2></th>
        <th><h2>Birim Tutar</h2></th>
      </tr>
    </thead>
    <tbody>";

    $mail->Body .= "<tr align='left'>";
    foreach ($urun_adi as $urun) {
        $mail->Body .= "<td bgcolor='#f0f8ff'><h2>$urun</h2></td>";
    }
    foreach ($gramaj_adetler as $gramaj_adet) {
        $mail->Body .= "<td bgcolor='#f0f8ff'><h2>$gramaj_adet</h2></td>";
    }
    foreach ($toplamadet as $toplamA) {
        $mail->Body .= "<td bgcolor='#f0f8ff'><h2>$toplamA</h2></td>";
    }
    foreach ($trBirimfiyat as $trBirimF) {
        $mail->Body .= "<td bgcolor='#f0f8ff'><h2>$trBirimF</h2></td>";
    }
    foreach ($trBirimtutar as $trBirimT) {
        $mail->Body .= "<td bgcolor='#f0f8ff'><h2>$trBirimT</h2></td>";
    }a
    $mail->Body .= "</tr>";
    $mail->Body .= "</tbody></table></div></div>"; //close table
    echo "</tr></thead><tbody><tr>";

3 Answers3

2

Just try this

$mail->Body = "<div class='container-fullwidth'>
<h2>Sipariş Özeti</h2>
<hr>
<div class='table-responsive'>          
  <table width='100%' class='table table-striped'>
    <thead>
      <tr>
        <th><h2>Ürün</h2></th>
        <th><h2>Gramaj</h2></th>
        <th><h2>Adet</h2></th>
        <th><h2>Birim Fiyat</h2></th>
        <th><h2>Birim Tutar</h2></th>
      </tr>
    </thead>
    <tbody>";

    for($i=0; $i < count($urn_adi); $i++) {
        $mail->Body .= "<tr align='left'>";
        $mail->Body .= "<td bgcolor='#f0f8ff'><h2>$urun_adi[$i]</h2></td>";
        $mail->Body .= "<td bgcolor='#f0f8ff'><h2>$gramaj_adetler[$i]</h2></td>";
        $mail->Body .= "<td bgcolor='#f0f8ff'><h2>$toplamadet[$i]</h2></td>";
        $mail->Body .= "<td bgcolor='#f0f8ff'><h2>$trBirimfiyat[$i]</h2></td>";
        $mail->Body .= "<td bgcolor='#f0f8ff'><h2>$trBirimtutar[$i]</h2></td>";
        $mail->Body .= "</tr>";
    }
    $mail->Body .= "</tbody></table></div></div>"; //close table
    echo "</tr></thead><tbody><tr>";
Mahipal Patel
  • 543
  • 3
  • 15
1

You should pack your Items into an Array in a Format like this:

$basket = array(array('quantity' => 1, 'articleid' => 123, 'foo' => 'something'),
                array('quantity' => 2, 'articleid' => 234, 'foo' => 'something'),
                array('quantity' => 3, 'articleid' => 345, 'foo' => 'something'));

When you have this Format, you can easily create a List with it.

echo '<table>';
foreach($basket as $item){
 echo '<tr><td>'.$item['quantity'].'</td><td>'.$item['articleid'].'</td><td>'.$item['foo'].'</td></tr>';
}
echo '</table>';

So I would suggest you to first merge these Arrays into one.

Bernhard
  • 1,852
  • 11
  • 19
1

You can 'zip' the arrays (as long as they are the same length), and then iterate through.

<?php
$letters = ['a', 'b', 'c', 'd'];
$numbers = [1, 2, 3, 4];

$zipped = array_map(null, $letters, $numbers);

$out = '<table>';
foreach($zipped as $array) {
    $out .= '<tr>';
    $out .=   '<td>' . $array[0] . '</td>';
    $out .=   '<td>' . $array[1] . '</td>';
    $out .= '</tr>';
}
$out .= '</table>';

Table result:

a   1
b   2
c   3
d   4

Zipped array:

array (
  0 => 
  array (
    0 => 'a',
    1 => 1,
  ),
  1 => 
  array (
    0 => 'b',
    1 => 2,
  ),
  2 => 
  array (
    0 => 'c',
    1 => 3,
  ),
  3 => 
  array (
    0 => 'd',
    1 => 4,
  ),
)
Progrock
  • 7,373
  • 1
  • 19
  • 25