如何在多个foreach中创建一个表?

时间:2017-07-12 07:31:43

标签: php html5

我正在使用php mailer构建添加到卡片代码。

我正在使用电子邮件创建发送购物车项目。 在邮件正文五个foreach数组中,我需要在那里创建一个表但是表行无法纠正,我该如何解决?

    $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 个答案:

答案 0 :(得分:2)

试试这个

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

答案 1 :(得分:1)

您应该使用以下格式将项目打包到数组中:

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

如果您有此格式,则可以使用它轻松创建列表。

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

所以我建议你先将这些数组合并为一个。

答案 2 :(得分:1)

你可以'压缩'数组(只要它们长度相同),然后迭代。

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

表结果:

a   1
b   2
c   3
d   4

压缩数组:

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,
  ),
)