在CodeIgniter中创建类别和子类别系统通常涉及数据库设计和MVC架构的实现。类别系统是一种常见的分层数据结构,允许无限级嵌套的子类别。
推荐使用两种常见的设计模式:
CREATE TABLE categories (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
parent_id INT DEFAULT NULL,
FOREIGN KEY (parent_id) REFERENCES categories(id) ON DELETE CASCADE
);
CREATE TABLE categories (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
lft INT NOT NULL,
rgt INT NOT NULL,
level INT NOT NULL
);
邻接表更简单直观,适合大多数中小型应用;嵌套集适合需要频繁查询整个树结构的场景。
// application/models/Category_model.php
class Category_model extends CI_Model {
public function __construct() {
parent::__construct();
}
// 获取所有类别
public function get_categories() {
$query = $this->db->get('categories');
return $query->result();
}
// 获取子类别
public function get_subcategories($parent_id) {
$this->db->where('parent_id', $parent_id);
$query = $this->db->get('categories');
return $query->result();
}
// 添加新类别
public function add_category($data) {
$this->db->insert('categories', $data);
return $this->db->insert_id();
}
// 递归获取类别树
public function get_category_tree($parent_id = 0) {
$tree = array();
$this->db->where('parent_id', $parent_id);
$query = $this->db->get('categories');
foreach ($query->result() as $row) {
$subtree = $this->get_category_tree($row->id);
$tree[] = array(
'id' => $row->id,
'name' => $row->name,
'children' => $subtree
);
}
return $tree;
}
}
// application/controllers/Categories.php
class Categories extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('category_model');
}
public function index() {
$data['categories'] = $this->category_model->get_category_tree();
$this->load->view('categories_view', $data);
}
public function add() {
if ($this->input->post()) {
$data = array(
'name' => $this->input->post('name'),
'parent_id' => $this->input->post('parent_id')
);
$this->category_model->add_category($data);
redirect('categories');
}
$data['categories'] = $this->category_model->get_categories();
$this->load->view('add_category', $data);
}
}
<!-- application/views/categories_view.php -->
<ul>
<?php foreach ($categories as $category): ?>
<li>
<?php echo $category['name']; ?>
<?php if (!empty($category['children'])): ?>
<ul>
<?php foreach ($category['children'] as $child): ?>
<li><?php echo $child['name']; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<!-- application/views/add_category.php -->
<form method="post" action="<?php echo site_url('categories/add'); ?>">
<label>Category Name:</label>
<input type="text" name="name" required>
<label>Parent Category:</label>
<select name="parent_id">
<option value="0">-- No Parent --</option>
<?php foreach ($categories as $category): ?>
<option value="<?php echo $category->id; ?>">
<?php echo $category->name; ?>
</option>
<?php endforeach; ?>
</select>
<button type="submit">Add Category</button>
</form>
原因:邻接表模式在深度嵌套时递归查询可能导致性能下降
解决方案:
原因:直接删除父类别可能导致子类别成为孤儿
解决方案:
解决方案: 在表中添加排序字段:
ALTER TABLE categories ADD COLUMN sort_order INT DEFAULT 0;
然后在查询时按此字段排序:
$this->db->order_by('sort_order', 'ASC');
对于大型应用,可以考虑:
以上实现提供了在CodeIgniter中创建类别和子类别系统的基本框架,可以根据具体需求进行调整和扩展。
没有搜到相关的文章