我的数据库中有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种或更多类别,它应该自动计算必须生成多少列。
任何帮助都将不胜感激。
发布于 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
<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>
结果:

https://stackoverflow.com/questions/53905555
复制相似问题