【问题标题】:table is creating 1 row instead of creating 9 rows表正在创建 1 行而不是创建 9 行
【发布时间】:2018-12-24 01:53:16
【问题描述】:

我的数据库中有 67 个类别,我使用 $categories 调用这些类别。

我想将它们全部动态打印到表格中。

到目前为止我所尝试的:

<table class="mdl-data-table mdl-js-data-table mdl-shadow--2dp">
<thead>
    <tr>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
    </tr>
</thead>
  <tbody>
      <tr>
        <?php
            $countCat  = round(count($categories) / 10); // 67 / 10 = 6. 7 | with round = 7
            $i = 0;
            foreach ($categories as $key => $value) { 
                ++$i;
                if ($i >= 10) {

        ?>
      <td class="mdl-data-table__cell--non-numeric">
        <label class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect" for="<?php print $value['catID']; ?>">
          <input type="checkbox" id="<?php print $value['catID']; ?>" class="mdl-checkbox__input">
          <span class="mdl-checkbox__label"><?php print $value['categoryNames']; ?></span>
        </label>
      </td>
      <?php
      }
      if ($i >= 20) { ?>

      <td class="mdl-data-table__cell--non-numeric">
        <label class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect" for="<?php print $value['catID']; ?>">
          <input type="checkbox" id="<?php print $value['catID']; ?>" class="mdl-checkbox__input">
          <span class="mdl-checkbox__label"><?php print $value['categoryNames']; ?></span>
        </label>
      </td>
      </tr>
        <?php

      }
        } 
      ?>

  </tbody>

结果:

即使使用 $i 停止并创建下一行,它也只输出 1 行?

我希望表格有 9 行,如果有 67 个或更多类别,它应该自动计算必须创建多少列。

任何帮助将不胜感激。

【问题讨论】:

  • 您缺少行的 标记?
  • 试过还是不行。

标签: php mysql html-table


【解决方案1】:

更新 2:

$countCat = ceil(count($categories) / 9); 计算你需要多少列。 => 否

在循环内每 N 个项目添加 &lt;/tr&gt;&lt;tr&gt; 标记,以在 html 表中创建新行。例如,您可以对每 N 个项目使用模 N (% N) 进行归档。

++$i 被移动到循环的底部,在末尾递增。

要在第一个循环中忽略 &lt;/tr&gt;&lt;tr&gt; 的创建,您可以使用:&amp;&amp; $i !== 0

<table class="mdl-data-table mdl-js-data-table mdl-shadow--2dp">
<thead>
    <tr>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
    </tr>
</thead>
<tbody>
    <tr>
        <?php
        $countCat = ceil(count($categories) / 9);
        $i = 0;
        foreach ($categories as $key => $value) {

            ?>

            <?php if ($i % $countCat === 0 && $i !== 0) { ?></tr><tr><?php } ?>

            <td class="mdl-data-table__cell--non-numeric">
                <label class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect" for="<?php print $value['catID']; ?>">
                    <input type="checkbox" id="<?php print $value['catID']; ?>" class="mdl-checkbox__input">
                    <span class="mdl-checkbox__label"><?php print $value['categoryNames']; ?></span>
                </label>
            </td>


            <?php
            ++$i;
        }

        ?>
    </tr>
</tbody>

结果:

【讨论】:

  • 嗯,你到底想要什么?一行有 10 个单元格,然后下一行有 10 个单元格?你能用更多细节更新你上面的问题吗? :)
  • 更新问题
  • 等我再次更改我的答案,没有读到你一直想要 9 行。
  • 更新了我的答案(更新 2)。
猜你喜欢
  • 2019-09-15
  • 1970-01-01
  • 2015-11-21
  • 2019-05-08
  • 2012-08-14
  • 2015-09-08
  • 2019-03-19
  • 1970-01-01
  • 2016-10-27
相关资源
最近更新 更多