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.
Each sub category has a parent_id in my database table category
Category Table
Category Description Table
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>