PHP中的无限级分类、无限嵌套评论

2020-08-28,

/2020/08/a41b6a98.png

一般来说,递归被称为函数自身的调用。

递归在开发中的实际运用

N级分类

无限级的分类在平常的开发中是常见的需求,并且在不少面试题中都会碰到。不管你做什么项目,应该都碰到过类似的问题。下面,我们就使用递归的思想,实战一把。

  • SQL结构

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;

然后我们虚拟出一些数据出来,最后长这个样子。

/2020/08/65df0d8a.png" /2020/08/65df0d8a.png" alt="Snipaste_2019-06-19_13-04-46.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的子目录,然后使用开始递归调用。最后的输出是这个样子的。

/2020/08/dccfd66b.png" /2020/08/dccfd66b.png" alt="Snipaste_2019-06-19_13-05-59.png

无限嵌套评论

先来看下这个 无限嵌套评论长什么样子。如图:

/2020/08/c20aad01.png" /2020/08/c20aad01.png" alt="Snipaste_2019-06-19_13-06-49.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中的无限级分类、无限嵌套评论.doc》

下载本文的Word格式文档,以方便收藏与打印。