0
votes

I am creating a categories list for my project.

When I view my list on my admin the sub categories show parent category as sub categories for some reason? The parent categories have chevron right on it in image.

Question how can I make sure that the correct sub categories are showing below there parent category.

enter image description here

Each sub category has a parent_id in my database table category

Category Table

enter image description here

Category Description Table

enter image description here

Controller

<?php

class Category extends MX_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('admin/catalog/model_category');
    }

    public function add() {
        if ($this->input->server('REQUEST_METHOD') == 'POST') {
            $this->model_category->add();
            redirect('admin/catalog/category');
        }

        $this->get_form();
    }

    public function edit() {
        if ($this->input->server('REQUEST_METHOD') == 'POST') {
            $this->model_category->edit();
            redirect('admin/catalog/category');
        }

        $this->get_form();
    }

    public function index() {
        $this->get_list();
    }

    public function get_list() {
        $this->document->setTitle('Categories');

        $data['heading_title'] = 'Categories';

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

        $results = $this->model_category->getCategories();

        foreach($results as $result){

            $sub_results = $this->model_category->getSubcategories($result['category_id']);

            foreach ($sub_results as $sub_result) {
                $data['subcategories'][] = array(
                    'category_id' => $result['category_id'],
                    'name' => $result['name']
                );
            }

            $data['categories'][] = array(
                'category_id' => $result['category_id'],
                'name' => $result['name']
            );
        }

        $data['header'] = Modules::run('admin/common/header/index');
        $data['footer'] = Modules::run('admin/common/footer/index');

        $this->load->view('template/catalog/category_list_view', $data);
    }
}

Model

<?php

class Model_category extends CI_Model {

    public function get_category() {
        $this->db->select('*');
        $this->db->from($this->db->dbprefix . 'category');
        $this->db->where('category_id', $this->uri->segment(5));
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return $query->row_array();
        } else {
            return FALSE;
        }
    }

    public function getCategories() {
        $this->db->select('*');
        $this->db->from($this->db->dbprefix . 'category_description cd', 'LEFT');
        $this->db->join($this->db->dbprefix . 'category c', 'c.category_id = cd.category_id', 'LEFT');
        $this->db->where('c.parent_id', '0');
        $query = $this->db->get();
        return $query->result_array();
    }

    function getSubcategories($parent_id) {
        $this->db->select('*');
        $this->db->from($this->db->dbprefix . 'category_description');
        $this->db->where('category_id', $parent_id);
        $query = $this->db->get();
        return $query->result_array();
    } 
}

View

<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<h4>Categories</h4>
<div class="list-group categories">
<?php foreach($categories as $category) { ?>

<div class="list-group-item"><?php echo $category['name']; ?><span class="glyphicon glyphicon-chevron-right"></span></div>

<?php foreach($subcategories as $subcategory) { ?>
<div class="list-group-item">
<?php echo $subcategory['name']; ?>
</div>
<?php }?>

<?php }?>
</div>
</div>
</div>
1
Just to confirm, is category a self-joined table? I mean the parent_id is referencing category_id of the same table?Musa Haidari
Can you see if you can get this controller method working? If so I will add the explanation. bitbucket.org/snippets/musahaidari/r95yEMusa Haidari
On the sub categories now I get error Undefined index: nameMr. ED
Have you changed the view according to new controller method?Musa Haidari
Yes did what you did but subcategory on view now saying Undefined index: nameMr. ED

1 Answers

0
votes

Thanks to all for advice.

I have figured out the best way to create what I am after.

Here is the working out put view image

enter image description here

First instead of having two tables I created one table to make it easy.

Then on my model I changed my functions to

function get_categories(){
    return $this->db->get_where('category', array('parent_id' => '0'))->result_array();
}

function get_sub_categories($category_id){
    return $this->db->get_where('category', array('parent_id' => $category_id))->result_array();
}

And then on controller

<?php

class Category extends MX_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('admin/catalog/model_category');
    }


    public function index() {
        $this->get_list();
    }

    public function get_list() {
        $this->document->setTitle('Categories');

        $data['heading_title'] = 'Categories';

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

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

        foreach ($categories as $category) {

            $sub_category_data = array();

            $sub_categories = $this->model_category->get_sub_categories($category['category_id']);

            foreach ($sub_categories as $sub_category) {
                $sub_category_data[] = array(
                    'category_id' => $sub_category['category_id'],
                    'name' => $category['name'] . '&nbsp;&nbsp;&gt;&nbsp;&nbsp;' . $sub_category['name'],
                    'edit' => site_url('admin/catalog/category/edit/' . $sub_category['category_id'])
                );
            }

            $data['categories'][] = array(
                'category_id' => $category['category_id'],
                'name' => $category['name'],
                'sub_categories' => $sub_category_data,
                'edit' => site_url('admin/catalog/category/edit/' . $category['category_id'])
            );
        }

        $data['header'] = Modules::run('admin/common/header/index');
        $data['footer'] = Modules::run('admin/common/footer/index');

        $this->load->view('template/catalog/category_list_view', $data);
    }
}

Then on view

<?php echo $header;?>
<div class="container">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="page-header">
<h1><?php echo $heading_title;?></h1>
</div>
</div>
</div>

<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="panel panel-default">

<div class="panel-heading">
<div class="clearfix">
<div class="pull-left">
<h1 class="panel-title"></h1>
</div>
<div class="pull-right">
<a href="<?php echo base_url('admin/catalog/category/add');?>" role="button" class="btn btn-success">Add New Category</a>
</div>
</div>
</div>

<div class="panel-body">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<td>Category Name</td>
<td class="text-right">Action</td>
</tr>
</thead>
<tbody>
<?php if ($categories) {?>
<?php foreach($categories as $category) : ?>
<tr class="category">
<td><?php echo $category['name']; ?></td>
<td class="text-right"><a href="<?php echo $category['edit'];?>" role="button" class="btn btn-primary">Edit</a></td>
</tr>
<?php foreach($category['sub_categories'] as $subcategory) : ?>
<tr>
<td><?php echo $subcategory['name']; ?></td>
<td class="text-right"><a href="<?php echo $subcategory['edit'];?>" role="button" class="btn btn-primary">Edit</a></td>
</tr>
<?php endforeach;?>
<?php endforeach;?>
<?php }?>
</tbody>
</table>
</div>

<div class="panel-footer">
</div>

</div>
</div>
</div>

</div>
<?php echo $footer;?>