在我的桌子上显示我数据库中的所有类别
如下图所示,category id
为1
还有两个类别parent category id
,1
也是
问题:如果任何类别的父类别ID匹配 另一个类别将它们显示在该类别下。
目前表格似乎
模型
public function get_categories() {
$data = array();
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'category');
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $result) {
$data[] = array(
'category_id' => $result['category_id'],
'parent_category_id' => $result['parent_category_id'],
'name' => $result['name'],
'url' => $result['url'],
'status' => $result['status'],
'date_added' => $result['date_added'],
'category_delete' => anchor('admin/category/delete/' . $result['category_id'], 'Delete', array('class' => 'btn btn-danger btn-block')),
'category_edit' => anchor('admin/category/update/' . $result['category_id'], 'Edit', array('class' => 'btn btn-primary btn-block'))
);
}
} else {
return false;
}
return $data;
}
控制器
<?php
class Category extends MY_Controller {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
}
public function index() {
$this->dynamic->set_title('Category');
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'active' => '',
'href' => anchor('admin/dashboard', 'Home')
);
$data['breadcrumbs'][] = array(
'active' => 'class="active"',
'href' => 'Category'
);
$this->load->library('table');
$template = array(
'table_open' => '<table class="table table-striped table-bordered">',
'thead_open' => '<thead>',
'thead_close' => '</thead>',
'heading_row_start' => '<tr>',
'heading_row_end' => '</tr>',
'heading_cell_start' => '<th>',
'heading_cell_end' => '</th>',
'tbody_open' => '<tbody>',
'tbody_close' => '</tbody>',
'row_start' => '<tr>',
'row_end' => '</tr>',
'cell_start' => '<td>',
'cell_end' => '</td>',
'row_alt_start' => '<tr>',
'row_alt_end' => '</tr>',
'cell_alt_start' => '<td>',
'cell_alt_end' => '</td>',
'table_close' => '</table>'
);
$this->table->set_heading(array('Category ID', 'Parent ID', 'Category Name', 'Category URL', 'Category Status', 'Category Date Added', 'Category Delete', 'Category Edit'));
$this->table->set_template($template);
$data['categories'] = $this->table->generate($this->get_categories());
$data['header'] = Modules::run('admin/common/header/index');
$data['footer'] = Modules::run('admin/common/footer/index');
$data['timeout'] = Modules::run('admin/common/timeout/index');
$data['navbar'] = Modules::run('admin/common/navbar/index');
$this->load->view('template/catalog/category_view', $data);
}
public function get_categories() {
$data = array();
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'category');
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $result) {
$data[] = array(
'category_id' => $result['category_id'],
'parent_category_id' => $result['parent_category_id'],
'name' => $result['name'],
'url' => $result['url'],
'status' => $result['status'],
'date_added' => $result['date_added'],
'category_delete' => anchor('admin/category/delete/' . $result['category_id'], 'Delete', array('class' => 'btn btn-danger btn-block')),
'category_edit' => anchor('admin/category/update/' . $result['category_id'], 'Edit', array('class' => 'btn btn-primary btn-block'))
);
}
} else {
return false;
}
return $data;
}
}
答案 0 :(得分:0)
在获取类别功能中,按来电添加订单。
$this->db->select('*'); $this->db->from($this->db->dbprefix . 'category'); $this->db->order_by('parent_category_id asc , category_id asc'); $query = $this->db->get();
答案 1 :(得分:0)
我不得不在模型中使用多个foreach循环。
<?php
class Category_model extends CI_Model {
public function get_categories() {
$data = array();
// 1st level category
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'category');
$this->db->where('parent_id', '0');
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $result) {
$data[] = array(
'name' => $result['name'],
'url' => $result['url'],
'status' => ($result['status']) ? 'Enabled' : 'Disabled',
'date_added' => $result['date_added'],
'category_delete' => anchor('admin/category/delete/' . $result['category_id'], 'Delete', array('class' => 'btn btn-danger btn-block')),
'category_edit' => anchor('admin/category/update/' . $result['category_id'], 'Edit', array('class' => 'btn btn-primary btn-block'))
);
// 2nd level category
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'category');
$this->db->where('sub_id', '0');
$this->db->where('parent_id', $result['category_id']);
$query = $this->db->get();
foreach ($query->result_array() as $result) {
$parent_category_name = $this->get_parent_category_name($result['parent_id']);
$data[] = array(
'name' => '<b>' . $parent_category_name->name . '</b>' . ' > '. $result['name'],
'url' => $result['url'],
'status' => ($result['status']) ? 'Enabled' : 'Disabled',
'date_added' => $result['date_added'],
'category_delete' => anchor('admin/category/delete/' . $result['category_id'], 'Delete', array('class' => 'btn btn-danger btn-block')),
'category_edit' => anchor('admin/category/update/' . $result['category_id'], 'Edit', array('class' => 'btn btn-primary btn-block'))
);
// 3rd level category
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'category');
$this->db->where('sub_id', $result['category_id']);
$query = $this->db->get();
foreach ($query->result_array() as $result) {
$parent_category_name = $this->get_parent_category_name($result['parent_id']);
$sub_category_name = $this->get_parent_category_name($result['sub_id']);
$data[] = array(
'name' => '<b>' . $parent_category_name->name . '</b>' . ' > '. '<b>' . $sub_category_name->name . '</b>' . ' > '. $result['name'],
'url' => $result['url'],
'status' => ($result['status']) ? 'Enabled' : 'Disabled',
'date_added' => $result['date_added'],
'category_delete' => anchor('admin/category/delete/' . $result['category_id'], 'Delete', array('class' => 'btn btn-danger btn-block')),
'category_edit' => anchor('admin/category/update/' . $result['category_id'], 'Edit', array('class' => 'btn btn-primary btn-block'))
);
}
}
}
return $data;
} else {
return false;
}
}
public function get_parent_category_name($parent_category_id) {
$this->db->where('category_id', $parent_category_id);
$query = $this->db->get($this->db->dbprefix . 'category');
if ($query->num_rows() > 0) {
return $query->row();
} else {
return false;
}
}
public function get_sub_category_name($sub_category_id) {
$this->db->where('category_id', $sub_category_id);
$query = $this->db->get($this->db->dbprefix . 'category');
if ($query->num_rows() > 0) {
return $query->row();
} else {
return false;
}
}
}