Steven Serrano Steven Serrano - 3 months ago 11
HTML Question

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:

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)

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

Answer

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

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.