首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >表正在创建1行而不是创建9行。

表正在创建1行而不是创建9行。
EN

Stack Overflow用户
提问于 2018-12-23 17:10:09
回答 1查看 38关注 0票数 0

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

我想把它们全部动态地打印到一个桌子上。

到目前为止,我已经尝试过:

代码语言:javascript
复制
<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种或更多类别,它应该自动计算必须生成多少列。

任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-23 17:15:59

更新2:

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

添加</tr><tr>标记循环中的每个N项,以在html表中创建新行。例如,可以对每个N项使用模N (% N)对其进行存档。

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

若要忽略在第一个循环中创建</tr><tr>,可以使用:&& $i !== 0

代码语言:javascript
复制
<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>

结果:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53905555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档