Use 'function recursion' to implement dynamic tree menu based on PHP and MySQL
Tree menus are widely used in many desktop application systems. Its main advantage is that the structure is clear, which helps users clearly know their current location. However, in the application of tree menus on the web, there are no ideal ready-made components that can be used directly. Therefore, in general, programmers mainly use JavaScript to implement some simple tree structure menus, but these menus are often prepared in advance. Defining each menu item and the hierarchical relationship between each menu item is not conducive to expansion. Once another menu structure is needed, it often needs to be rewritten, so it is not very convenient to use.
After studying function recursion, I found that this kind of tree menu can dynamically change the display of the tree menu through recursive functions, and there is no limit on the number of series. The following is the processing code for a dynamic tree menu I wrote using php, MySQL, and JavaScript. If you are interested, come with me to see how I implement it:)
First of all, we need a database. In this database, we create the following table:
CREATE TABLE menu (
id tinyint(4) NOT NULL auto_increment,
parent_id tinyint(4) DEFAULT '0' NOT NULL,
name varchar(20),
url varchar( 60),
PRIMARY KEY (id)
);
In this table
id is the index
parent_id is used to save the id number of the previous menu, if it is a first-level menu it is 0
name is the name of the menu, that is, The menu content displayed on the page
url If a menu is the last-level menu, you need to specify the url address of the connection. This field is used to save this address. For other non-last-level menus, this field is empty
Okay, Once the database is available, you can add some records. Here are some records I used when doing tests:
INSERT INTO menu VALUES ('1', '0', 'Personnel Management', '');
INSERT INTO menu VALUES ( '2', '0', 'Communication', '');
INSERT INTO menu VALUES ( '3', '1', 'File Management', '');
INSERT INTO menu VALUES ( '4', '1', 'Attendance Management', 'http://localhost/personal/attendance.php');
INSERT INTO menu VALUES ( '5', '2', 'Address Book', '') ;
INSERT INTO menu VALUES ( '6', '2', 'Online meeting', '');
INSERT INTO menu VALUES ( '7', '3', 'Add file', 'http://localhost /personal/add_achive.php');
INSERT INTO menu VALUES ( '8', '3', 'Search archive', 'http://localhost/personal/search_archive.php');
INSERT INTO menu VALUES ( ' 9', '3', 'Delete archive', 'http://localhost/personal/delete_archive.php');
INSERT INTO menu VALUES ( '10', '5', 'Add communication record',' http://localhost/communication/add_address.php');
For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software