


PHP implements levelless classification of tree structure_PHP tutorial
Two methods to implement tree structure
1. Recursive method
Recursion is when a function explicitly calls itself.
The characteristics of using the recursive method to realize the tree structure are that the data writing speed is faster and the display speed is slower (especially obvious when the tree has many branches/levels). It is suitable for situations where the amount of written data is large and the tree structure is complex.
Data structure (take mysql as an example)
Code:------------------------------------------------ --------------------------------
CREATE TABLE `tree1` (
`id` tinyint(3) unsigned NOT NULL auto_increment,
`parentid` tinyint(3) unsigned NOT NULL default '0',
`topic` varchar(50) default NULL,
PRIMARY KEY (`id`),
KEY `parentid` (`parentid`)
) TYPE=MyISAM;
INSERT INTO `tree1` (`id`, `parentid`, `topic`) VALUES
(1,0,'Tree 1'),
(2,0,'tree2'),
(3,0,'Tree 3'),
(4,2,'Tree 2-1'),
(5,4,'Tree 2-1-1'),
(6,2,'Tree 2-2'),
(7,1,'Tree 1-1'),
(8,1,'Tree 1-2'),
(9,1,'Tree 1-3'),
(10,8,'Tree 1-2-1'),
(11,7,'Tree 1-1-1'),
(12,11,'Tree 1-1-1-1');
-------------------------------------------------- ----------------------------------
Field Description
id, the recorded id number
parentid, the parent record id of the record (if it is 0, it is the root record)
topic, the record’s display title
Show program
Sequence tree:
PHP code:------------------------------------------------- ----------------------------------
/* Database connection */
mysql_connect();
mysql_select_db('tree');
/* Recursive function displayed in tree format */
function tree($parentid = 0) {
/*Execute sql query to obtain the title and id of the record*/
$sql = "select topic,id from tree1 where parentid = $parentid order by id asc";
$rs = mysql_query($sql);
/* Indent*/
echo("
- ");
- '.$ra[0].' ');
while($ra = mysql_fetch_row($rs)) {
/* Display record title */
echo('
/* Recursive call */
tree($ra[1]);
}
echo("
}
tree();
?>
-------------------------------------------------- ----------------------------------
Reverse order tree:
PHP code:------------------------------------------------- ----------------------------------
/* Database connection */
mysql_connect();
mysql_select_db('tree');
/* Recursive function displayed in tree format */
function tree($parentid = 0) {
/*Execute sql query to obtain the title and id of the record*/
$sql = "select topic,id from tree1 where parentid = $parentid order by id desc";
$rs = mysql_query($sql);
/* Indent*/
echo("
- ");
- '.$ra[0].' ');
while($ra = mysql_fetch_row($rs)) {
/* Display record title */
echo('
/* Recursive call */
tree($ra[1]);
}
echo("
}
tree();
?>
-------------------------------------------------- ----------------------------------
Insert data program
PHP code:------------------------------------------------- ----------------------------------
/* Database connection */
mysql_connect();
mysql_select_db('tree');
$sql = "insert into tree (topic,parentid) values('tree3-1',3);";
mysql_query($sql);
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
