Home > Backend Development > PHP Tutorial > Development process and example analysis of PHP unlimited classification

Development process and example analysis of PHP unlimited classification

黄舟
Release: 2023-03-15 12:58:01
Original
1412 people have browsed it

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(&#39;目录导航&#39;);  //实例化

//setNode(目录ID,上级ID,目录名字);
$Tree->setNode(1, 0, &#39;目录1&#39;);
$Tree->setNode(2, 1, &#39;目录2&#39;);
$Tree->setNode(3, 0, &#39;目录3&#39;);
$Tree->setNode(4, 3, &#39;目录3.1&#39;);
$Tree->setNode(5, 3, &#39;目录3.2&#39;);
$Tree->setNode(6, 3, &#39;目录3.3&#39;);
$Tree->setNode(7, 2, &#39;目录2.1&#39;);
$Tree->setNode(8, 2, &#39;目录2.2&#39;);
$Tree->setNode(9, 2, &#39;目录2.3&#39;);
$Tree->setNode(10, 6, &#39;目录3.3.1&#39;);
$Tree->setNode(11, 6, &#39;目录3.3.2&#39;);
$Tree->setNode(12, 6, &#39;目录3.3.3&#39;);
//getChilds(指定目录ID);
//取得指定目录下级目录.如果没有指定目录就由根目录开始
$category = $Tree->getChilds();
//遍历输出
foreach ($category as $key=>$id)
{
  echo $Tree->getLayer($id, &#39;|-&#39;).$Tree->getValue($id)."<br />\n";
}
?>
Copy after login

The running result is as shown below:

Development process and example analysis of PHP unlimited classification

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template