这篇文章主要介绍了关于php中的无限级分类和无限嵌套评论,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
上一篇文章我们讲到实战PHP数据结构基础之递归。来回顾下什么是递归?
一般来说,递归被称为函数自身的调用。
无限级的分类在平常的开发中是常见的需求,并且在不少面试题中都会碰到。不管你做什么项目,应该都碰到过类似的问题。下面,我们就使用递归的思想,实战一把。
SQL结构
立即学习“PHP免费学习笔记(深入)”;
CREATE TABLE `categories` ( `id` int(11) NOT NULL AUTO_INCREMENT, `categoryName` varchar(100) NOT NULL, `parentCategory` int(11) DEFAULT '0', `sortInd` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
然后我们虚拟出一些数据出来,最后长这个样子。
![1530949247750347.png 1067277681-5b3f6f03c13f1_articlex[1].png](https://img.php.cn//upload/image/850/809/524/1530949247750347.png)
下面 我们直接看代码实现。
<?php
$dsn = "mysql:host=127.0.0.1;port=3306;dbname=light-tips;charset=UTF8;";
$username = 'root';
$password = 'admin';
$pdo = new PDO($dsn, $username, $password);
$sql = 'SELECT * FROM `categories` ORDER BY `parentCategory`, `sortInd`';
$result = $pdo->query($sql, PDO::FETCH_OBJ);
$categories = [];
foreach ($result as $category) {
$categories[$category->parentCategory][] = $category;
}
function showCategoryTree($categories, $n)
{
if (isset($categories[$n])) {
foreach ($categories[$n] as $category) {
echo str_repeat('-', $n) . $category->categoryName . PHP_EOL;
showCategoryTree($categories, $category->id);
}
}
return;
}
showCategoryTree($categories, 0);可以看到,我们首先获取到了所有的数据,然后按照父级ID归类。这是一个非常棒的数据结构。想象一下,我们把展示顶级目录下所有子目录的问题分解成了展示自己的类目标题和展示数据中parentCategory为当前目录id的子目录,然后使用开始递归调用。最后的输出是这个样子的。
![1530949258455547.png 1120440599-5b3f6f024d406_articlex[1].png](https://img.php.cn//upload/image/175/531/307/1530949258455547.png)
先来看下这个 无限嵌套评论长什么样子。如图:
![1530949266844917.png 3082497495-5b3f6f026065f_articlex[1].png](https://img.php.cn//upload/image/522/329/758/1530949266844917.png)
上面的栗子,又是一个经典的可以使用递归解决的案例。还是来看下数据结构。
CREATE TABLE `comments` ( `id` int(11) NOT NULL AUTO_INCREMENT, `comment` varchar(500) NOT NULL, `username` varchar(50) NOT NULL, `datetime` datetime NOT NULL, `parentID` int(11) NOT NULL, `postID` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;
大家可以自己实践一遍,先不要看下面的内容。
<?php
$dsn = "mysql:host=127.0.0.1;port=3306;dbname=light-tips;charset=UTF8;";
$username = 'root';
$password = 'admin';
$pdo = new PDO($dsn, $username, $password);
$sql = 'SELECT * FROM `comments` WHERE `postID` = :id ORDER BY `parentId`, `datetime`';
$stmt = $pdo->prepare($sql);
$stmt->setFetchMode(PDO::FETCH_OBJ);
$stmt->execute([':id' => 1]);
$result = $stmt->fetchAll();
$comments = [];
foreach ($result as $comment) {
$comments[$comment->parentID][] = $comment;
}
function showComments(array $comments, $n)
{
if (isset($comments[$n])) {
foreach ($comments[$n] as $comment) {
echo str_repeat('-', $n) . $comment->comment . PHP_EOL;
showComments($comments, $comment->id);
}
}
return;
}
showComments($comments, 0);使用递归进行目录文件的扫描的栗子。
<?php
function showFiles(string $dir, array &$allFiles)
{
$files = scandir($dir);
foreach ($files as $key => $value) {
$path = realpath($dir . DIRECTORY_SEPARATOR . $value);
if (!is_dir($path)) {
$allFiles[] = $path;
} else if ($value != "." && $value != "..") {
showFiles($path, $allFiles);
$allFiles[] = $path;
}
}
return;
}
$files = [];
showFiles('.', $files);
foreach ($files as $file) {
echo $file . PHP_EOL;
}以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
以上就是PHP中的无限级分类和无限嵌套评论的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号