0
votes

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
Did you use var_dum for both $rows from get_child_category() and get_parent_category() to see sql result?Shojaeddin
@shojaeddin yes, but how it solve my problem?Hamza Zafeer

3 Answers

0
votes

To me it seem you did not split your html and loops properly here:

        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>

Usually whenever you have a loop you should have some output before any nested loop started. In your case first level loop is about Categories which should become an <li> of parent main menu <ul>.

I think. You need to transform this fragment to:

    foreach($this->parent_category as $cat){ ?>
        <li ...>
          ...
          <ul ...> <?php
            foreach($this->child_category as $child_cat){ ?>
                <li>...</li> <?php
            } ?>
          </ul>
        </li> <?php
    }
0
votes

when you do not have repetitive value in $rows, why you use this part again for $cat['name']?

elseif($cat['parent_id']==0){
?>
    <li><a href="<?php echo $cat['url']?>"><span><?php echo $cat['name'];?></span></a></li>
<?php
            }

when in first part of foreach you create

<a class="dropdown-toggle" data-toggle="dropdown" href="#"><?php echo $cat['name'];?>
0
votes

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