当我们需要在网站中实现分类功能时,我们通常会使用无限级分类的方法。无限级分类指的是分类可以无限级嵌套,每一级分类下还可以有子分类。下面我将讲解如何使用 PHP 简单实现无限级分类的方法。
当我们需要在网站中实现分类功能时,我们通常会使用无限级分类的方法。无限级分类指的是分类可以无限级嵌套,每一级分类下还可以有子分类。下面我将讲解如何使用 PHP 简单实现无限级分类的方法。
步骤一:设计数据库
分类功能的实现离不开数据库,因此我们需要事先设计好数据库结构。常用的设计方式是使用两个表:一个表存储分类信息,另一个表存储分类之间的层级关系。
category表:
id int(10) unsigned NOT NULL AUTO_INCREMENT
name varchar(255) NOT NULL
PRIMARY KEY (`id`)
category_relationship表:
id int(10) unsigned NOT NULL AUTO_INCREMENT
parent_id int(10) unsigned NOT NULL COMMENT '上级分类ID'
child_id int(10) unsigned NOT NULL COMMENT '当前分类ID'
PRIMARY KEY (`id`)
我们可以看到,category_relationship表设计得非常简单,只有两个字段:parent_id和child_id,用于存储每个分类的上级分类和当前分类。
步骤二:查询分类
查询分类是无限级分类的关键,我们需要使用递归算法来实现,以下是示例代码:
function getCategoryTree($parentId = 0){
$result = [];
$categories = $this->db->query('SELECT * FROM category_relationship WHERE parent_id=' . $parentId);
if($categories->num_rows > 0){
while($category = $categories->fetch_assoc()){
$result[$category['child_id']] = $category;
$result[$category['child_id']]['children'] = $this->getCategoryTree($category['child_id']);
}
}
return $result;
}
这段代码的作用是:当传入的$parentId为0时,获取所有顶级分类,然后递归查询子分类,直到所有的子分类都被查询完成。关键的地方在于:在查询子分类之前,先将当前分类作为父分类存进$result数组中,再将查询到的子分类存进children字段中。
步骤三:显示分类
分类查询完成后,我们需要将分类信息显示在页面上。那么如何显示呢?以下是示例代码:
function displayCategoryTree($tree, $depth = 0){
foreach($tree as $category){
echo str_repeat('-', $depth) . $category['name'] . '<br/>';
if(isset($category['children'])){
$this->displayCategoryTree($category['children'], $depth + 1);
}
}
}
这段代码的作用是:用递归的方式遍历分类信息并将分类名输出,每个子分类的层级比父分类的层级深一级,用横杠作为缩进符号。
示例一:电商网站商品分类
我们可以在电商网站中使用无限级分类来实现商品分类功能,以下是示例代码:
<?php
class Category {
private $db;
public function __construct(mysqli $db){
$this->db = $db;
}
public function getCategoryTree($parentId = 0){
$result = [];
$categories = $this->db->query('SELECT * FROM category_relationship WHERE parent_id=' . $parentId);
if($categories->num_rows > 0){
while($category = $categories->fetch_assoc()){
$result[$category['child_id']] = $category;
$result[$category['child_id']]['children'] = $this->getCategoryTree($category['child_id']);
}
}
return $result;
}
public function displayCategoryTree($tree, $depth = 0){
foreach($tree as $category){
echo str_repeat('-', $depth) . $category['name'] . '<br/>';
if(isset($category['children'])){
$this->displayCategoryTree($category['children'], $depth + 1);
}
}
}
}
$db = new mysqli('localhost', 'user', 'password', 'database');
$category = new Category($db);
$categoryTree = $category->getCategoryTree();
$category->displayCategoryTree($categoryTree);
示例二:博客网站文章分类
我们也可以在博客网站中使用无限级分类来实现文章分类功能,以下是示例代码:
<?php
class Category {
private $db;
public function __construct(mysqli $db){
$this->db = $db;
}
public function getCategoryTree($parentId = 0){
$result = [];
$categories = $this->db->query('SELECT * FROM category_relationship WHERE parent_id=' . $parentId);
if($categories->num_rows > 0){
while($category = $categories->fetch_assoc()){
$result[$category['child_id']] = $category;
$result[$category['child_id']]['children'] = $this->getCategoryTree($category['child_id']);
}
}
return $result;
}
public function displayCategoryTree($tree, $depth = 0){
foreach($tree as $category){
echo str_repeat('-', $depth) . $category['name'] . '<br/>';
if(isset($category['children'])){
$this->displayCategoryTree($category['children'], $depth + 1);
}
}
}
}
$db = new mysqli('localhost', 'user', 'password', 'database');
$category = new Category($db);
$categoryTree = $category->getCategoryTree();
$category->displayCategoryTree($categoryTree);
以上就是 PHP 简单实现无限级分类的方法的完整攻略。
本文标题为:PHP简单实现无限级分类的方法


基础教程推荐
- PHP实现限制域名访问的实现代码(本地验证) 2023-05-01
- PHP实现的猴王算法(猴子选大王)示例 2022-10-15
- PHP设计模式之中介者模式(Mediator Pattern)入门与应用案例详解 2023-03-17
- php通过array_unshift函数添加多个变量到数组前端的方法 2023-12-24
- php自动获取关键字的方法 2023-08-04
- 浅析PHP7 的垃圾回收机制 2023-02-05
- PHP一个简单的无需刷新爬虫 2022-12-04
- php从数组中随机抽取一些元素的代码 2023-12-25
- laravel利用中间件防止未登录用户直接访问后台的方法 2023-02-21
- PHP排序二叉树基本功能实现方法示例 2022-10-19