我需要根据他们的类别显示帖子标题。但问题是设计。它有以下div结构。我正在使用PHP。
<div class="row">
<div class="some-class">
<h1> Category1 name here</h1>
<ul>
<li>sds</li>
<li>sds</li>
<li>sds</li>
</ul>
</div>
<div class="some-class">
<h1> Category2 name here</h1>
<ul>
<li>sds</li>
<li>sds</li>
<li>sds</li>
</ul>
</div>
</div>
我知道我需要使用foreach循环,但不知道以哪种方式 这是我得到的数组。
Array
(
[0] => Array
(
[category_name] => Getting Started
[id] => 2
[question] => GS1
[answer] => a sd asd asdjasld asd asdasd asd asd asd asdjkalsjd as
dasd asd asd as das d. as dasd asd as
asd asd asd asd as das as d.
a sdas d asd as dasd asd asd . asd asss
.a sd asd asd as
[created] => 2017-02-17 05:26:15
[slug] => gs1
[category] => 3
)
[1] => Array
(
[category_name] => Getting Started
[id] => 5
[question] => GS2
[answer] => sad as d as
[created] => 2017-02-17 05:48:19
[slug] => gs2
[category] => 3
)
[2] => Array
(
[category_name] => Buying & Selling
[id] => 3
[question] => BS1
[answer] => sadas dasdas d
as d
as dasdkas;ldk;laskd; kasd
as dlask ;ldk; kas;dk asd lka;sdk
[created] => 2017-02-17 05:47:33
[slug] => bs1
[category] => 4
)
[3] => Array
(
[category_name] => Buying & Selling
[id] => 6
[question] => BS2
[answer] => as das das d
[created] => 2017-02-17 05:48:39
[slug] => bs2
[category] => 4
)
[4] => Array
(
[category_name] => Renting
[id] => 4
[question] => rent1
[answer] => asd asd as das
[created] => 2017-02-17 05:48:06
[slug] => rent1
[category] => 6
)
)
答案 0 :(得分:1)
首先更改您的QUERY以使用GROUP BY返回名称和类别,试试这个..
SELECT faq_categories.category_name, GROUP_CONCAT (site_faqs.question) FROM faq_categories JOIN site_faqs ON site_faqs.category = faq_categories.id GROUP BY faq_categories.category_name ORDER BY faq_categories.id ASC
将值作为数组获取并尝试以下代码
<div class="row">
<?php foreach( $categoryArr as $category ) { ?>
<div class="some-class">
<h1> <?php echo $category['category_name']; ?> </h1>
<ul>
<?php $questions = explode (', ', $category['question'] ); // explode the comma seperated question value to array
foreach( $questions as $question ){ ?>
<li> <?php echo $question; ?> </li>
<?php } ?>
</ul>
</div>
<?php } ?>
</div>
答案 1 :(得分:1)
将上次使用的category
保存为$catt
,然后检查下次类别名称是否相等。
如果不相等意味着它显示标题并且<li>
也显示。
否则它只在该标题的子类别中显示<li>
。
感谢Magnus。
测试:
<div class="row">
<?php
$catt = '';
foreach( $arr as $category ) {
if($catt != $category){
?>
<div class="some-class">
<h1> <?php echo $category;?></h1>
<ul>
<li>sds</li>
<li>sds</li>
<li>sds</li>
</ul>
</div>
<?php
} else{ ?>
<ul>
<li>sds</li>
<li>sds</li>
<li>sds</li>
</ul>
<?php }
$catt=$category;
}?>
</div>
答案 2 :(得分:0)
希望它会对你有所帮助。
步骤:
第1步:从类别表中获取类别
第2步:根据类别ID
从帖子表中获取帖子代码:这是示例代码,请根据您的表格和代码进行修改
<div class="row">
<?php
$categories = " SELECT * From Category " ;
foreach ( $categories as $category ) {
$posts = " select * from posts where category = ".$category->id;
if(count($posts) > 0) {
?>
<div class="some-class">
<h1> <?php echo $category->name; ?></h1>
<ul>
<?php
foreach ( $posts as $post ) { ?>
<li> <?php echo $post->title; ?> </li>
<?php } ?>
</ul>
</div>
<?php } } ?>
</div>
注意:这就是您想要使用的方式。请根据您的代码进行修改。