On my controller I have a variable called 'name' => $child['name'], for my drop down menu.

The problem I am having is that in the child dropdown menu it still shows parent name.

I am trying to make it so if parent_id name is equal 0 then will not show in dropdown child links.

Why is it still showing parent name in dropdown child links

Controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Test extends MX_Controller {

    public function index() {
        $this->load->model('admin/catalog/model_catalog_category');

        $data['categories'] = array();

        $categories = $this->get_categories(0);

        foreach ($categories as $category) {
            if ($category['top']) {

                $children = $this->model_catalog_category->get_categories($category['category_id']);

                foreach ($children as $child) {
                    // Level 2
                    $children_data[] = array(
                        'name' => $child['name'],
                        'href' => site_url('admin/test') .'/'. strtolower($category['name']) .'/'. strtolower($child['name'])
                    );
                }

                // Level 1
                $data['categories'][] = array(
                    'name' => $category['name'],
                    'children' => $children_data,
                    'column'   => $category['column'] ? $category['column'] : 1,
                    'href' => site_url('admin/test') .'/'. strtolower($category['name'])
                );
            }
        }

        $this->load->view('template/test/test_category_view', $data);
    }

    public function get_categories($parent_id = 0) {
        $this->db->where('parent_id', $parent_id);
        $query = $this->db->get('category');
        if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return false;
        }
    }
}

View

<?php if ($categories) { ?>
<div class="container">
    <nav id="menu" class="navbar">
    <div class="navbar-header">
    <span id="category" class="visible-xs">Menu</span>
        <button type="button" class="btn btn-navbar navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse"><i class="fa fa-bars"></i></button>
    </div>
    <div class="collapse navbar-collapse navbar-ex1-collapse">
        <ul class="nav navbar-nav">
            <?php foreach ($categories as $category) { ?>

                <?php if ($category['children']) { ?>

                <li class="dropdown">
                <a href="<?php echo $category['href']; ?>" class="dropdown-toggle" data-toggle="dropdown">
                <?php echo $category['name']; ?>
                </a>

                <div class="dropdown-menu">

                    <div class="dropdown-inner">

                        <?php foreach (array_chunk($category['children'], ceil(count($category['children']) / $category['column'])) as $children) { ?>
                        <ul class="list-unstyled">
                        <?php foreach ($children as $child) { ?>
                        <li><a href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?></a></li>
                        <?php } ?>
                        </ul>
                        <?php } ?>
                    </div>

                <a href="<?php echo $category['href']; ?>" class="see-all">See All <?php echo $category['name']; ?></a> 

                </div><!-- Drop Down Menu -->

                </li>

                <?php } else { ?>

                    <li><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a></li>

                <?php } ?> 

            <?php } ?>

        </ul>

        </div>

    </nav>

</div>

<?php } ?>

Change your codings for if ($categories) into if (isset($categories) && !empty($categories))
Then if ($category['children']) using same feature and try again.

I tried that no luck very strange but most lilkey a simple fix.

I have solved issue I now use $this->get_categories($category['category_id']); for the child links in controller. When loaded by model function was not working so updated with the function on controller and now working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.