【发布时间】:2015-09-22 18:43:32
【问题描述】:
我正在为我的项目创建一个类别列表。
当我在我的管理员上查看我的列表时,子类别出于某种原因将父类别显示为子类别?父类别在图像中带有 V 形符号。
问题如何确保正确的子类别显示在父类别下方。
每个子类别在我的数据库表类别
中都有一个 parent_id
分类表
类别描述表
控制器
<?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);
}
}
型号
<?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();
}
}
查看
<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>
【问题讨论】:
-
只是为了确认一下,类别是自联接表吗?我的意思是 parent_id 引用的是同一张表的 category_id?
-
你能看看你能不能让这个控制器方法工作?如果是这样,我将添加解释。 bitbucket.org/snippets/musahaidari/r95yE
-
现在在子类别上出现错误未定义索引:名称
-
您是否根据新的控制器方法更改了视图?
-
是的,你做了什么,但视图上的子类别现在显示未定义索引:名称
标签: php codeigniter