使用标签和 accordion 显示结果

sqyvllje  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(288)

我有两张table( posts , categories ),两个表之间存在一对多关系(每个类别可以包含多个post)。

posts table
___________________________________________________
|    |       |         |             |             |  
| id | title | content | category_id | posts_order |                                        
|____|_______|_________|_____________|_____________|
|    |       |         |             |             |    
| 1  | test1 | testing |     1       |       0     |   
|____|_______|_________|_____________|_____________|
|    |       |         |             |             |    
| 2  | test2 | testing |     1       |       1     |   
|____|_______|_________|_____________|_____________|
|    |       |         |             |             |    
| 3  | test3 | testing |     2       |       2     |   
|____|_______|_________|_____________|_____________|
|    |       |         |             |             |    
| .  | ..... | ....... |     .       |       .     |   
|____|_______|_________|_____________|_____________|

       categories table
___________________________
|      |        |         |  
| c_id | c_name | c_order |                                       
|______|________|_________|
|      |        |         |   
|   1  |  cat1  |    0    |      
|______|________|_________|            
|   2  |  cat2  |    1    |      
|______|________|_________|
|      |        |         |       
|   3  |  cat3  |    2    |      
|______|________|_________|   
| .    | ..... | .......  |      
|______|_______|__________|

有一个关系(一对多),使用 id 从职位和 c_id 从类别。
我想在选项卡中显示类别,然后在每个选项卡中都会有相关的帖子:

________________________________________
|                 |          |          |
|    cat1         |   cat2   |   cat3   |
|  (active)       |__________|__________|
|                                       |
|                                       |
|    test1                              |
|        testing                        |
|                                       |
|    test2                              |
|        testing                        |
|_______________________________________|

例如,如果第一个选项卡处于活动状态,那么 cat1 ,它们是 test1 以及 test2 应该以 accordion 的形式展示这些内容。
我试过:

SELECT * FROM `categories` LEFT JOIN `posts` ON categories.c_id = posts.id order by categories.c_order ASC

然后将数据打印到选项卡和accordion:

<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
    <?php foreach ($results as $result) { ?>
        <li role="presentation">
            <a href="#<?php echo $result['c_name'] ?>" aria-controls="<?php echo $result['c_name'] ?>" role="tab" data-toggle="tab">
                <?php echo $result['c_name'] ?>
            </a>
        </li>
    <?php } ?> //end foreach
</ul> <!-- Nav tabs -->

<!-- Tab panes -->
<div class="tab-content">
    <?php foreach ($results as $result) { ?>
        <div role="tabpanel" class="tab-pane" id="<?php echo $result['c_name'] ?>">
            <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true"> 
                <div class="panel panel-default">
                    <div class="panel-heading" role="tab" id="headingOne">
                        <h4 class="panel-title">
                            <a role="button" data-toggle="collapse" data-parent="#accordion" href="#<?php echo $result['id']; ?>_post" aria-expanded="true" aria-controls="collapseOne">
                                <?php echo $result['title'] ?>
                            </a>
                        </h4> <!-- .panel-title  -->
                    </div> <!-- .panel-heading  -->
                    <div id="<?php echo $result['id']; ?>_post" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
                        <div class="panel-body">
                            <?php echo $result['content']; ?>
                        </div> <!-- .panel-body  -->
                    </div> <!-- .panel-collapse  -->
                </div> <!-- .panel  -->
            </div> <!-- .panel-group  -->
        </div> <!-- .tab-pane  -->
    <?php } ?> //end foreach
</div> <!-- end Tab panes -->

所以我应该得到3个标签,每个标签包含每个类别的帖子和内容。
但如果我有3个 categories 和9 posts 例如,我得到9个选项卡,每个选项卡包含1篇文章或根本没有文章。
查询是否有问题?
我该换衣服吗 foreach 地方?

koaltpgm

koaltpgm1#

而不是使用 $results = $results->fetchAll(); 您可以一次提取一行,并按类别对它们进行分组。

while ($row = $results->fetch(PDO::FETCH_ASSOC)) {
    $categories[$row['c_name']][] = $row;
}

然后循环打印选项卡可以如下所示:

// for the tabs, you just need the keys (the distinct categories)
foreach (array_keys($categories) as $category_name) { ...

对于选项卡内容:

// the tab panes
foreach ($categories as $category_name => $posts) {
    // accordion container
    foreach ($posts as $post) {
        // individual accordion panels

为了不模糊逻辑,我没有加入标记。如果您在如何将其集成到标记中遇到问题,请告诉我,我会尽力澄清。

相关问题