php foreach 循环在 tr 中重复 td 2 次

php foreach loop repeat td's 2 times in a tr

我正在创建一个 PDF 来打印用户地址。我有以下 HTML 结构:

我试过以下代码:

<?php   $i=0; ?>
    <tr valign="top">
        foreach ($users as $user): ?>
        <?php if ($i % 2 == 1) {?>
            <tr valign="top">
        <?php } ?>

但现在可以正常工作了。

<table width="700" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr valign="top">
    <td width="48%" align="left" valign="top" style="padding:10px 10px 15px; font-size:14px; color:#333; font-family:Verdana, Geneva, sans-serif; border:1px solid #ccc;"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="22%" height="25" valign="bottom">Name</td>
        <td width="78%" valign="bottom" style="border-bottom:1px dashed #ccc;">&nbsp;</td>
        </tr>
      <tr>
        <td height="40" valign="bottom">&nbsp;</td>
        <td valign="bottom" style="border-bottom:1px dashed #ccc;">&nbsp;</td>
        </tr>
      <tr>
        <td height="40" valign="bottom">Phone</td>
        <td valign="bottom" style="border-bottom:1px dashed #ccc;">&nbsp;</td>
        </tr>
    </table></td>
    <td width="4%" align="left">&nbsp;</td>
    <td width="48%" align="left" style="padding:10px 10px 15px; font-size:14px; color:#333; font-family:Verdana, Geneva, sans-serif; border:1px solid #ccc;"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="22%" height="25" valign="bottom">Name</td>
        <td width="78%" valign="bottom" style="border-bottom:1px dashed #ccc;">&nbsp;</td>
      </tr>
      <tr>
        <td height="40" valign="bottom">&nbsp;</td>
        <td valign="bottom" style="border-bottom:1px dashed #ccc;">&nbsp;</td>
      </tr>
      <tr>
        <td height="40" valign="bottom">Phone</td>
        <td valign="bottom" style="border-bottom:1px dashed #ccc;">&nbsp;</td>
      </tr>
    </table></td>
  </tr>
  <tr valign="top">
    <td height="15" align="left"></td>
    <td height="15" align="left"></td>
    <td height="15" align="left"></td>
  </tr>
</table>

在上面table我想在每个TR中重复TD 2次并在每个TR之后添加空白TR。

上面的HTML输出应该是下图:

谁能帮我解决这个问题。

如果我正确地找到你要找的东西:I want to repeat in one TR repeat two TD 然后这个代码:

<?php
error_reporting(E_ALL); ini_set('display_errors', 1);

echo"<table>";

for ($i = 1; $i <= 10; $i++) {

if(($i % 2) == 1)  // odd -> start TR
  { echo "<tr><td class=\"dark\">$i</td>"; }
else   // even -> close TR 
  { echo "<td class=\"red\">$i</td></tr><tr><td colspan=\"2\">whatever here</tr>"; }
}

echo"</table>";
?>

会给你这个输出:

<table>
<tr><td class="dark">1</td><td class="red">2</td></tr><tr><td colspan="2">whatever here</tr>
<tr><td class="dark">3</td><td class="red">4</td></tr><tr><td colspan="2">whatever here</tr>
<tr><td class="dark">5</td><td class="red">6</td></tr><tr><td colspan="2">whatever here</tr>
<tr><td class="dark">7</td><td class="red">8</td></tr><tr><td colspan="2">whatever here</tr>
<tr><td class="dark">9</td><td class="red">10</td></tr><tr><td colspan="2">whatever here</tr>
</table>

评论后编辑:添加了一个 'blank' TR

尝试以下您错过的解决方案以关闭

<?php
            $i=0; 
            foreach ($users as $user){
            if ($i % 2 == 0) {?>
                <tr valign="top">
            <?php } ?>
        <td width="48%" align="left" style="padding:10px 10px 15px; font-size:14px; color:#333; font-family:Verdana, Geneva, sans-serif; border:1px solid #ccc;"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="22%" height="25" valign="bottom">Name</td>
            <td width="78%" valign="bottom" style="border-bottom:1px dashed #ccc;">&nbsp;</td>
          </tr>
          <tr>
            <td height="40" valign="bottom">&nbsp;</td>
            <td valign="bottom" style="border-bottom:1px dashed #ccc;">&nbsp;</td>
          </tr>
          <tr>
            <td height="40" valign="bottom">Phone</td>
            <td valign="bottom" style="border-bottom:1px dashed #ccc;">&nbsp;</td>
          </tr>
        </table></td>
            <?php if ($i % 2 == 0) {?>
                </tr>
            <?php } }?>