我有2个表(// Near the beginning of your program:
std::ios_base::sync_with_stdio(false);
// ...
{
// Prefer using ostringstream if you never need to read from it
std::ostringstream ss;
// std::endl is never needed. Use '\n' instead. If you want to flush,
// explicitly write `ss << '\n' << std::flush`. For stringstreams, I believe
// it doesn't matter, but it's good to get into the habit of doing this
ss << stringA << " 1 " << 5 << 'C' << '\n';
std::lock_guard<std::mutex> lock(mutex);
std::cout << ss.rdbuf();
}
,posts
),两个表之间存在一对多的关系(每个类别可能包含一个以上的帖子)。
categories
存在一个关系(一对多),使用帖子中的 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
然后将数据打印到标签和手风琴上
SELECT * FROM `categories` LEFT JOIN `posts` ON categories.c_id = posts.id order by categories.c_order ASC
我应该获得3个标签,每个标签包含每个类别的帖子和内容。
但是例如,如果我有3个<!-- 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 -->
和9个categories
,我将获得9个标签,每个标签包含1个帖子或根本没有帖子。
查询是否有问题?
我应该改变posts
的位置吗?
答案 0 :(得分:1)
代替使用$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
我没有添加标记,以免混淆逻辑。如果您在查看如何将其集成到标记中时遇到麻烦,请告诉我,我会尽力澄清。