因此,我不确定它是否需要我的Frach循环或SQL查询需要修复,但是我尝试通过“CyryNyMy”进行分组,但没有效果。我希望类别名称($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....

代码:
$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>

代码:(更新)
<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-->

最佳答案

您可以检查当前类别是否与上一个类别相同。因此您可以相应地打开和关闭ul
例如

<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

10-08 11:44