仅显示具有特定ID的数据数据库

时间:2018-12-23 04:20:08

标签: php html if-statement

我有以下代码可显示数据库中的所有数据:

<?php foreach($alldatas as $data): ?>
    <div class="col-md-4">
        <div class="card">
            <div class="content">
                <h3><?php echo $data['data_name'];?></h3>
                <div class="footer text-center">
                <?php echo $data['data_version'];?>
                </div>
            </div>
        </div>
    </div>
<?php endforeach; ?>

该代码非常适合以不同的data_namedata_version显示我的所有数据。但是我想要的是仅显示具有特定ID的数据。

所以我想使其与引导navtabs一起使用:

<div class="col-md-12">
    <ul class="nav nav-pills nav-tabs nav-justified" role="tablist">
        <li role="presentation" class="active"><a href="#pro" aria-controls="pro" role="tab" data-toggle="tab">Pro</a></li>
        <li role="presentation"><a href="#middle" aria-controls="middle" role="tab" data-toggle="tab">Middle</a></li>
        <li role="presentation"><a href="#low" aria-controls="low" role="tab" data-toggle="tab">Low</a></li>
    </ul>
</div>

<?php foreach($alldatas as $data): ?>
    <div class="tab-content">
        <div role="tabpanel" class="tab-pane fade in active" id="pro">
            <?php if($data['id'] == "2" | $data['id'] == "3" | $data['id'] == "4"){?>
                <div class="col-md-4">
                    <div class="card">
                        <div class="content">
                            <h3><?php echo $data['data_name'];?></h3>
                            <div class="footer text-center">
                            <?php echo $data['data_version'];?>
                            </div>
                        </div>
                    </div>
                </div>
            <?php } ?>
        </div>

        <div role="tabpanel" class="tab-pane fade" id="middle">
            <?php if($data['id'] == "5" | $data['id'] == "6" | $data['id'] == "7"){?>
                <div class="col-md-4">
                    <div class="card">
                        <div class="content">
                            <h3><?php echo $data['data_name'];?></h3>
                            <div class="footer text-center">
                            <?php echo $data['data_version'];?>
                            </div>
                        </div>
                    </div>
                </div>
            <?php } ?>
        </div>
    </div>
<?php endforeach; ?>

但是当我尝试单击其他选项卡时,它仅显示ID为1,2,3的数据。该如何解决?

1 个答案:

答案 0 :(得分:0)

您在“ tab-content”上有foreach循环,这是错误的,实际上,它将创建许多“ tab-content”,而这并不是必需的。您必须循环查找非“ tab-content”内容。

因此,第一个解决方法是:

 <div class="tab-content">

    <div role="tabpanel" class="tab-pane fade in active" id="pro">
        <?php foreach($alldatas as $data): ?>
            <?php if($data['id'] == "2" | $data['id'] == "3" | $data['id'] == "4"){?>
                <div class="col-md-4">
                    <div class="card">
                        <div class="content">
                            <h3><?php echo $data['data_name'];?></h3>
                            <div class="footer text-center">
                                <?php echo $data['data_version'];?>
                            </div>
                        </div>
                    </div>
                </div>
            <?php } ?>
        <?php endforeach; ?>
    </div>

    <div role="tabpanel" class="tab-pane fade" id="middle">
        <?php foreach($alldatas as $data): ?>
            <?php if($data['id'] == "5" | $data['id'] == "6" | $data['id'] == "7"){?>
                <div class="col-md-4">
                    <div class="card">
                        <div class="content">
                            <h3><?php echo $data['data_name'];?></h3>
                            <div class="footer text-center">
                                <?php echo $data['data_version'];?>
                            </div>
                        </div>
                    </div>
                </div>
            <?php } ?>
        <?php endforeach; ?>
    </div>
</div>

它将解决您的问题,但是,如果仍然存在,则可以实现jquery函数以自定义切换“活动”类。 (不过,请先按照上面的说明纠正“ foreach”的位置)。