【问题标题】:foreach() table with empty space?foreach() 表有空白空间?
【发布时间】:2018-03-01 22:08:19
【问题描述】:

我有以下 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; ?>

这没有区别(仍然是空格):

【问题讨论】:

  • 所以基本上,你想摆脱表格中的空值吗?我无法查看您的图像,因为它们在我当前的网络上被阻止。您应该在问题中明确说明您要做什么。
  • 不太确定您的问题是什么,但也许只是删除额外的&lt;tbody&gt;?将它们移到foreachs(两者)之外,以便您只有其中一个
  • “我与 Col-lg-12 合作” - 你实际上与 col-lg-2 合作
  • 整个是 col-lg-12,表格得到 lg-2,因为我希望每个“行”有 6 个
  • 我认为使用ul 比使用cols 更好地实现你想要的效果

标签: php html css twitter-bootstrap html-table


【解决方案1】:

我认为您从数据库中为每个类别获得不同数量的链接,这会导致空白。尝试按类别回显您获得的链接数量:

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

【讨论】:

  • 是的,我知道这一点:) 还是我误解了你?但我希望表直接在下一张表下排序i.imgur.com/Zkfulbb.png
  • 哦,很抱歉误解了您的问题。现在很清楚你想要实现什么。我不是 CSS 方面的专家,但我认为这是不可能的。无论如何,我会让 CSS 大师们发表意见...
猜你喜欢
  • 2012-11-02
  • 2012-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-15
  • 2016-07-25
  • 2021-01-10
  • 1970-01-01
相关资源
最近更新 更多