我有以下 PHP 代码包含 2 个 foreachs
<div class="box">
<?php
$category = ORM::for_table('cattable')->order_by_asc('order')->find_many();
foreach ($category as $cat):
$links = ORM::for_table('linktable')->order_by_asc('name')->where('idcat', $cat->id)->find_many();
?>
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-2" >
<table class="table table-hover table-nomargin table-condensed">
<thead>
<tr>
<td><?php echo $cat->name?></td>
</tr>
</thead>
<?php foreach ($links as $link): ?>
<tbody>
<tr class="odd gradeX">
<td><?php echo $link->name?></td>
</tr>
</tbody>
<?php endforeach;
foreach ($links as $link): ?>
<tbody>
<tr class="odd gradeX">
<?php if($link->main==7)
{
?>
<td><?php echo $link->name?></td>
<?php
}
?>
</tr>
</tbody>
<?php endforeach; ?>
</table>
</div>
<?php endforeach; ?>
</div>
问题是,我确实有很多空格,而且表格没有按我想要的方式显示..
我和 Col-lg-12 一起工作,我想让所有的表都像这样显示:
但它显示如下:
我还有很多空条目
编辑:
?><?php foreach ($categorie as $cat):
$links = ORM::for_table('linktable')->order_by_asc('name')->where('idcat', $cat->id)->limit(10)->find_many();
?>
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-2">
<table class="table table-hover table-nomargin table-condensed">
<thead>
<tr>
<td>
<b>test</b>
</td>
</tr>
</thead>
<tbody>
<?php foreach ($links as $link): ?>
<tr>
<td>test</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endforeach; ?>
这没有区别(仍然是空白):
最佳答案
我认为您从数据库中为每个类别获得了不同数量的链接,这导致了空白。尝试按类别回应您获得的链接数:
?><?php foreach ($categorie as $cat):
$links = ORM::for_table('linktable')->order_by_asc('name')->where('idcat', $cat->id)->limit(10)->find_many();
?>
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-2">
<table class="table table-hover table-nomargin table-condensed">
<thead>
<tr>
<td>
<b>test</b>
// Print the number of links by category
<?php echo count($links); ?>
</td>
</tr>
</thead>
<tbody>
<?php foreach ($links as $link): ?>
<tr>
<td>test</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endforeach; ?>
关于php - foreach()表有空白吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46328959/