In our daily work, we often encounter many classifications. We know that many open source software’s infinite classifications use recursive algorithms, but we know that recursion is a waste of time and space (memory). This article introduces PHP The infinite classification tree method obtains the lower-level directories of the specified directory. If no directory is specified, it starts from the root directory and finally traverses the output.
First download the PHP infinite classification tree method we need to use in this course: http://www.php.cn/xiazai/leiku/404
Find us after the download is complete Unzip the required php class files to our local directory and create a new php file!
After completion, we need to call this class in the new php file and instantiate the class:
<?php include_once "fenlei.php";//引入类文件 //new Tree(根目录的名字); //根目录的ID自动分配为0 $Tree = new Tree('目录导航'); //实例化 //setNode(目录ID,上级ID,目录名字); $Tree->setNode(1, 0, '目录1'); $Tree->setNode(2, 1, '目录2'); $Tree->setNode(3, 0, '目录3'); $Tree->setNode(4, 3, '目录3.1'); $Tree->setNode(5, 3, '目录3.2'); $Tree->setNode(6, 3, '目录3.3'); $Tree->setNode(7, 2, '目录2.1'); $Tree->setNode(8, 2, '目录2.2'); $Tree->setNode(9, 2, '目录2.3'); $Tree->setNode(10, 6, '目录3.3.1'); $Tree->setNode(11, 6, '目录3.3.2'); $Tree->setNode(12, 6, '目录3.3.3'); //getChilds(指定目录ID); //取得指定目录下级目录.如果没有指定目录就由根目录开始 $category = $Tree->getChilds(); //遍历输出 foreach ($category as $key=>$id) { echo $Tree->getLayer($id, '|-').$Tree->getValue($id)."<br />\n"; } ?>
The running result is as shown below:
The above is the detailed content of Development process and example analysis of PHP unlimited classification. For more information, please follow other related articles on the PHP Chinese website!