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

时间:2017-04-29 08:52:34

标签: php html foreach cakephp-2.0

我创建了一个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>

在abolve表中,我想在每个TR中重复TD 2次,并在每次TR后添加空白TR。

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

enter image description here

任何人都可以帮助我解决这个问题。

2 个答案:

答案 0 :(得分:0)

如果我找到了您正在寻找的内容: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>

评论后编辑:添加一个空白&#39; TR

答案 1 :(得分:0)

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

<?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 } }?>