所以我不确定它的foreach循环或我的sql查询是否需要修复,但我尝试按" categoryname"进行分组。无济于事。我希望类别名称($ qs [' name'])只显示一次,并且所有属于该类别的li都显示在其中。任何帮助都会很棒。 我有这个:
NEW HAMPSHIRE <--This is $state_name-->
Personal auto <--This is $qs['name']-->
• Your young drivers—help them play it safe
Personal auto <--This is $qs['name']-->
• The dangers of drunken driving
Personal auto <--This is $qs['name']-->
• How to keep your vehicle safe from car thieves
ETC....
而我所说的是:
NEW HAMPSHIRE <--This is $state_name-->
Personal auto <--This is $qs['name']-->
• Your young drivers—help them play it safe
• The dangers of drunken driving
• How to keep your vehicle safe from car thieves
ETC....
CODE:
$sql = mysql_query(
"SELECT qs, state, title, link, category, categoryid, categoryname
FROM `irc_consumer_content`
JOIN `irc_consumer_content_category`
ON irc_consumer_content.category=irc_consumer_content_category.categoryid
ORDER BY categoryname ASC;");
$row = mysql_fetch_assoc($sql);
$states_array = array( "AL"=>"Four-state topics",
"CT"=>"Connecticut topics",
"NH"=>"New Hampshire topics",
"NJ"=>"New Jersey topics",
"NY"=>"New York topics");
do {
$qs_array[$row['state']][] = array( "qs"=>$row['qs'],
"title"=>$row['title'],
"link"=>$row['link'],
"name"=>$row['categoryname']);
} while ($row = mysql_fetch_assoc($sql));
<div>
<h1>Topics available</h1>
<? //==== GET STATE ====================================
foreach ($states_array as $state => $state_name) { ?>
<h2><? echo $state_name; ?></h2>
<? //==== GET STATE TOPICS ======================
foreach ($qs_array[$state] as $i => $qs) { ?>
<h3><? echo $qs['name']; ?></h3>
<ul class="first-level">
<li class="list" id="<? echo $qs['qs']; ?>">
<div class="selected-list-item">
<div id="title-text">
<? echo iconv('Windows-1252', 'UTF-8', $qs['title']); ?>
</div>
<div class="ideas-div">
<div class="ideas-top">
</div>
<div class="idea-images">
</div>
<div class="ideas-bottom">
</div>
</div>
</div>
</li>
</ul>
<? } ?>
<? } ?>
</div>
CODE:(修订版)
<div>
<h1>Topics available</h1>
<? //==== GET STATE ====================================
foreach ($states_array as $state => $state_name) { ?>
<h2><? echo $state_name; ?></h2>
<? $category = "";?>
<? //==== GET STATE TOPICS ======================
foreach ($qs_array[$state] as $i => $qs) {
if ($category != $qs['name']){
$category = $qs['name'];
?>
<h3><? echo $category; //echo $qs['name']; ?></h3>
<ul class="first-level">
<li class="list" id="<? echo $qs['qs']; ?>">
<? }else { ?>
</li>
</ul>
<ul class="first-level">
<li class="list" id="<? echo $qs['qs']; ?>">
<? }?>
<!--ALL ELSE IS THE SAME-->
答案 0 :(得分:1)
您可以检查当前类别是否与前一个类别相同。因此,您可以相应地打开和关闭ul
。
e.g。
<div>
<h1>Topics available</h1>
<? //==== GET STATE ====================================
foreach ($states_array as $state => $state_name) { ?>
<h2><? echo $state_name; ?></h2>
<?php $old_name = ''; ?>
<ul>
<? //==== GET STATE TOPICS ======================
foreach ($qs_array[$state] as $i => $qs) { ?>
<?php if ($old_name != $qs['name']) : ?>
<?php $old_name = $qs['name']; ?>
</ul><--Close previous item ul-->
</ul><--Open ul for new item-->
<?php endif; ?>
<h3><? echo $qs['name']; ?></h3>
<ul class="first-level">
<li class="list" id="<? echo $qs['qs']; ?>">
<div class="selected-list-item">
<div id="title-text">
<? echo iconv('Windows-1252', 'UTF-8', $qs['title']); ?>
</div>
<div class="ideas-div">
<div class="ideas-top">
</div>
<div class="idea-images">
</div>
<div class="ideas-bottom">
</div>
</div>
</div>
</li>
<? } ?>
</ul>
<? } ?>
</div>
另请注意,mysql*
函数已弃用。请改用PDO
。