douren1928 2019-04-16 14:20
浏览 20
已采纳

每个子类别的类别都在重复

I am showing categories in Menu. Some categories have subcategories.

function for getting parent categories

function get_parent_category(){
        $query="select * from blog_categories where parent_id=0 
                ORDER BY 
                CASE id 
                    WHEN '2' THEN 1 
                    WHEN '1' THEN 2 
                    WHEN '3' THEN 3
                    ELSE id 
                END";
        $rows=array();
        $result=$this->query($query);
        while($row=$this->fetch_array($result)){
            $row['url']=$this->get_cat_url($row);
            $rows[]=$row;
        }
        return $rows;
    }

Function for subcategories

function get_child_category(){
        $query="select * from blog_categories where parent_id!=0";
        $rows=array();
        $result=$this->query($query);
        while($row=$this->fetch_array($result)){
            $row['url']=$this->get_cat_url($row);
            $rows[]=$row;
        }
        return $rows;
}

Showing on the page like this:

<ul class="nav navbar-nav">
    <li><a href="<?php echo BASE_URL ?>">Home</a></li>
    <?php 
         foreach($this->parent_category as $cat){
            foreach($this->child_category as $child_cat){
                if($cat['id']==$child_cat['parent_id']){
    ?>
        <li class="dropdown">
            <a class="dropdown-toggle" data-toggle="dropdown" href="#"><?php echo $cat['name'];?>
            <span class="caret"></span></a>
            <ul class="dropdown-menu">
                <li><a href="#"><?php echo $child_cat['name']?></a></li>
            </ul>
        </li>
    <?php
                }elseif($cat['parent_id']==0){
    ?>
        <li><a href="<?php echo $cat['url']?>"><span><?php echo $cat['name'];?></span></a></li>
    <?php
                }
     ?>

    <?php }}?>

Output and Problem

enter image description here

The Main category circle in red color is seerah which has two subcategories. showing two times for first one in drop down one subcategory and for second time second subcategory is showing.

DB structure

enter image description here

What i wants:

I wants to show each subcategories under each parent category without repetition , how can i achieve this?

  • 写回答

3条回答 默认 最新

  • doufenyu7610 2019-04-17 15:15
    关注

    Here is how i handled the problem

    <?php 
             foreach($this->parent_category as $cat){
                 $html = '';
                 foreach($this->child_category as $child_cat){
                    if($cat['id']==$child_cat['parent_id']){
                        $html .=  '<li><a href="'.$child_cat['url'].'">' . $child_cat['name'] . '</a></li>';
                        // here is all child categories are saved in var.
                    }
                 }
    
                 if ($html == '') {
        ?>
                    <li><a href="<?php echo $cat['url']?>"><span><?php echo $cat['name'];?></span></a></li>
        <?php
                } else {
        ?>
                <li class="dropdown">
                    <a class="dropdown-toggle" data-toggle="dropdown" href="#"><?php echo $cat['name'];?>
                    <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <?php echo $html; ?> // here is displayed under parent category
                    </ul>
                </li>
        <?php
                }
            }
        ?>
    

    Output

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件