我有2个表posts
和categories
,两个表之间有one-to-many
关系。
这是两个表:
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
之间存在关系,因此category_id
表中的posts
引用类别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代码,用于为每个帖子创建标签和手风琴,如果有帮助,请参见完整代码:
<!-- 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 -->
我应该获得所有活动的categories
和posts
,但是我只能获得第一个活动类别,并按顺序排列帖子。
答案 0 :(得分:1)
我将推荐此查询:
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
INNER JOIN
,因为我希望posts
中的所有行在categories
中都有对应的行。c_order
然后按post_order
的顺序排列,以便在迭代结果集时将分类条目分组在一起。基于我的演示:http://sqlfiddle.com/#!9/4ca1a1/3
并引用fetchAll
的{{3}} ...
$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)
中。
然后您可以迭代准备的结果集并访问第一级键和子数组行以显示数据。
答案 1 :(得分:-1)
您的查询必须是这样的:
SELECT * FROM `posts` `p`
LEFT JOIN `categories` `c` ON `c`.`c_id` = `p`.`category_id`
WHERE `c`.`c_active` = '1'
and `p`.`post_active` = '1'
order by `p`.`post_order`, `c`.`c_order`
**然后执行查询并放入数组**
$myArr = array();
foreach($youQuery as $key => $val){
$myArr[$val->c_id]['c_id '] = $val->c_id;
$myArr[$val->c_id]['c_name '] = $val->c_name;
$myArr[$val->c_id]['post '][$val->id]['id] = $val->id;
$myArr[$val->c_id]['post '][$val->title ]['title ] = $val->title ;
$i++;
}
然后,您可以根据需要循环数组。