我尝试创建一个带有类别选择器的区域,并且每个类别都要显示帖子。我把类别,以及来自mysql数据库的帖子和我在php页面上显示的那些。我也使用Bootstrap 4。
我的代码:
<div class="col center_content">
<div class="categories_buttons">
<small id="emailHelp" class="form-text text-muted">Choose a category:</small>
<p>
<?php
$category_select = 'SELECT * FROM categories';
$posts_select = 'SELECT * FROM categories, posts WHERE posts.post_categ_id=categories.categ_id';
$category_query = $DBcon->query($category_select);
$category_query2 = $DBcon->query($category_select);
$posts_query = $DBcon->query($posts_select);
if(isset($category_query)){
while($category_row = $category_query->fetch_assoc()){
?>
<button class="btn btn-primary btn-sm" type="button" data-toggle="collapse" data-target="#collapseCategory-<?= $category_row['categ_id'];?>" aria-expanded="false" aria-controls="collapseCategory-<?= $category_row['categ_id']; ?>">
<?= $category_row['categ_name'] ?>
</button>
<?php
}
}
?>
</p>
</div>
<div class="clasa_cu_probleme">
<?php
if(isset($category_query2)){
while($category_row2 = $category_query2->fetch_assoc()){
if(isset($posts_query)){
while($posts_row = $posts_query->fetch_assoc()){
?>
<div class="collapse_div collapse" id="collapseCategory-<?=$category_row2['categ_id'];?>">
<div class="card card-block">
<?php echo $posts_row['post_name'];
?>
</div>
</div>
<?php
}
}
}
}
?>
</div>
</div>
答案 0 :(得分:0)
您需要为每个类别更新$posts_query
,因此您必须在$category_row2
循环的每次迭代中更新它。我无法测试解决方案,但它看起来像这样:
<div class="col center_content">
<div class="categories_buttons">
<small id="emailHelp" class="form-text text-muted">Choose a category:</small>
<p>
<?php
$category_select = 'SELECT * FROM categories';
// this is moved to below
// $posts_select = 'SELECT * FROM categories, posts WHERE posts.post_categ_id=categories.categ_id';
// $posts_query = $DBcon->query($posts_select);
$category_query = $DBcon->query($category_select);
$category_query2 = $DBcon->query($category_select);
if(isset($category_query)){
while($category_row = $category_query->fetch_assoc()){
?>
<button class="btn btn-primary btn-sm" type="button" data-toggle="collapse" data-target="#collapseCategory-<?= $category_row['categ_id'];?>" aria-expanded="false" aria-controls="collapseCategory-<?= $category_row['categ_id']; ?>">
<?= $category_row['categ_name'] ?>
</button>
<?php
}
}
?>
</p>
</div>
<div class="clasa_cu_probleme">
<?php
if(isset($category_query2)){
while($category_row2 = $category_query2->fetch_assoc()){
// This is where it gets updated with the new categ_id, then run the query
$posts_select = 'SELECT * FROM categories, posts WHERE posts.post_categ_id='.category_row2['categ_id'];
$posts_query = $DBcon->query($posts_select);
if(isset($posts_query)){
while($posts_row = $posts_query->fetch_assoc()){
?>
<div class="collapse_div collapse" id="collapseCategory-<?=$category_row2['categ_id'];?>">
<div class="card card-block">
<?php echo $posts_row['post_name'];
?>
</div>
</div>
<?php
}
}
}
}
?>
</div>
</div>
修改强>
这是一个更好的解决方案,因为您只需要运行一次类别查询:
<div class="col center_content">
<div class="categories_buttons">
<small id="emailHelp" class="form-text text-muted">Choose a category:</small>
<p>
<?php
$category_select = 'SELECT * FROM categories';
// this replaces the single query
$posts_selects = array()
$category_query = $DBcon->query($category_select);
if(isset($category_query)){
while($category_row = $category_query->fetch_assoc()){
// populate an array with page_queries values for later
$posts_selects[] = 'SELECT * FROM categories, posts WHERE posts.post_categ_id='.category_row['categ_id'];
?>
<button class="btn btn-primary btn-sm" type="button" data-toggle="collapse" data-target="#collapseCategory-<?= $category_row['categ_id'];?>" aria-expanded="false" aria-controls="collapseCategory-<?= $category_row['categ_id']; ?>">
<?= $category_row['categ_name'] ?>
</button>
<?php
}
}
?>
</p>
</div>
<div class="clasa_cu_probleme">
<?php
if(count($posts_selects) != 0){
foreach($posts_selects as $posts_select)
$posts_query = $DBcon->query($posts_select);
if(isset($posts_query)){
while($posts_row = $posts_query->fetch_assoc()){
?>
<div class="collapse_div collapse" id="collapseCategory-<?=$category_row2['categ_id'];?>">
<div class="card card-block">
<?php echo $posts_row['post_name'];
?>
</div>
</div>
<?php
}
}
}
}
?>
</div>
</div>