如何在UL中對表格值進行分組?

[英]How to group dulicate table values in UL?


So I'm not sure if its my foreach loop or my sql query that needs fixing, but Ive tried grouping by "categoryname" to no avail. I want the category names ($qs['name']) to be displayed only once and all li belonging to that category displayed in it. Any help would be great. What I have is this:

所以我不確定它是我的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....

And what I'm looinkg for is this:

而我所想要的是:

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:(UPDATED)

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-->

1 个解决方案

#1


1  

You can check if current category is the same with the previous one. So you can open and close the ul accordingly.

您可以檢查當前類別是否與前一個類別相同。因此,您可以相應地打開和關閉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>

Also keep in mind that mysql* functions are deprecated. Use PDO instead.

還要記住,不推薦使用mysql *函數。請改用PDO。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/08/23/19ef5dbf7e649e811ac52fb4563a3d29.html



 
  © 2014-2022 ITdaan.com 联系我们: