php - 按顺序选择事件数据

标签 php mysql sql pdo

我有两个表postscategories,两个表之间是一对多关系。

这是两个表:

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


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

category_idc_id之间存在关联,所以posts表中的category_id是指类别 ID c_id

post_orderc_order 用于对 postscategories 进行排序。

post_activec_active 用来定义是否显示给用户,1 表示是显示,而 0 为否。

我显示了 categoriesposts 如下:

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

显示数据的代码:

//That query should get the active categories and posts ordered by the order column, Not sure if it's correct. 
$results = $conn->prepare("SELECT * FROM categories LEFT JOIN categories ON categories.c_id = posts.id WHERE categories.c_active = '1' and posts.post_active = '1' order by categories.c_order, posts.post_order ASC");

//Execute the previous query.
$results->execute();

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

//Print the categories
foreach (array_keys($categories) as $category_name) {
    echo $category_name;
}

//Print the posts
foreach ($categories as $category_name => $posts) { 
    foreach ($posts as $post) { 
        echo $post['title'];
    }
}

之前的 PHP 代码中有 HTML 代码,用于为每个帖子创建选项卡和 Accordion ,如果有帮助,这里是完整代码:

<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
    <?php foreach (array_keys($categories) as $category_name) { ?>
        <li role="presentation"><a href="#<?php echo str_replace(' ', '', $category_name); ?>" aria-controls="<?php echo str_replace(' ', '', $category_name); ?>" role="tab" data-toggle="tab"><?php echo $category_name; ?></a></li>
    <?php } ?>
</ul> <!-- .nav-tabs -->

<!-- Tab panes -->
<div class="tab-content">
    <?php foreach ($categories as $category_name => $posts) { ?>
        <div role="tabpanel" class="tab-pane fade" id="<?php echo str_replace(' ', '', $category_name); ?>">
            <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true"> 
                <?php foreach ($posts as $post) { ?>
                    <div class="panel panel-default">
                        <div class="panel-heading" role="tab" id="headingOne">
                            <h4 class="panel-title">
                                <a role="button" class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#<?php echo $post['id']; ?>_faq" aria-expanded="false" aria-controls="#<?php echo $post['id']; ?>_faq">
                                    <?php echo $post['title'] ?>
                                </a>
                            </h4>
                        </div>
                        <div id="<?php echo $post['id']; ?>_faq" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
                            <div class="panel-body">
                                <?php echo $post['content']; ?>
                            </div> <!-- .panel-body -->
                        </div> <!-- .panel-collapse -->
                    </div> <!-- .panel-default -->
                <?php } ?>  
            </div> <!-- .panel-group -->
        </div> <!-- .tab-pane -->
    <?php } ?>
</div> <!-- .tab-content -->

我应该得到所有活跃的类别帖子,但我只得到第一个活跃的类别及其帖子。

最佳答案

我会推荐这个查询:

SELECT b.c_name, a.id, TRIM(a.title) AS title, a.content
FROM posts a
INNER JOIN categories b ON a.category_id = b.c_id
WHERE a.post_active = '1' AND b.c_active = '1'
ORDER BY b.c_order, a.post_order
  • 与其要求 php 修剪标题,不如在查询中修复它。
  • 我正在使用 INNER JOIN,因为我希望 posts 中的所有行在 categories 中都有相应的行。
  • c_order 然后按 post_order 排序,以便在迭代结果集时将分类条目组合在一起。

基于我的演示:http://sqlfiddle.com/#!9/4ca1a1/3

并引用 fetchAllFETCH_GROUP ...

$resultset = fetchAll(PDO::FETCH_GROUP);
/* should generate:
    $resultset = [
        "social" => [
            ["id" => 5, "title" => "fifth title", "content" => "fifth content"],
            ["id" => 4, "title" => "fourth title", "content" => "fourth content"],
            ["id" => 2, "title" => "second title", "content" => "second content"]
        ],
        "tech" => [
            ["id" => 1, "title" => "first title", "first content"]
        ]
    ];
*/

您的类别在 array_keys($resultset) 中。

并且您可以迭代准备好的结果集并访问第一级键和子数组行以显示数据。

关于php - 按顺序选择事件数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51940543/

相关文章:

php - 从命令行调用 PHP 时设置 $_SERVER 变量?

MYSQL 更新包含联接和子查询的表

Mysql子查询返回多行

php - MYSQL - 加入或子查询问题

mysql - 向查询添加 +1 以解决 "division by zero"错误

php - 在 PHP 的匿名函数中包装变量

php - 取消注释 php.ini 后,XAMPP 和 cURL 无法正常工作

php - Laravel 使用数据库数据填充表单但显示所有用户数据

mysql - 搜索三列

php - 将表的 id 调用到新表并将值插入到该新表中