我有两个表posts
和categories
,两个表之间是一对多
关系。
这是两个表:
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_id
和c_id
之间存在关联,所以posts
表中的category_id
是指类别 ID c_id
。
post_order
和 c_order
用于对 posts
和 categories
进行排序。
post_active
和c_active
用来定义是否显示给用户,1
表示是显示,而 0
为否。
我显示了 categories
和 posts
如下:
________________________________________
| | | |
| 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
并引用 fetchAll
的 FETCH_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/