Home  >  Article  >  Backend Development  >  Using "Function Recursion" to Implement Dynamic Tree Menu Based on PHP and MySQL_PHP Tutorial

Using "Function Recursion" to Implement Dynamic Tree Menu Based on PHP and MySQL_PHP Tutorial

WBOY
WBOYOriginal
2016-07-20 10:59:38762browse

​ 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, 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, which is the menu content to be 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, now that you have the database, you can add some records. Here are some records I used when doing testing:
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', 'Web 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');

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445580.htmlTechArticleTree menus are widely used in many desktop application systems. Its main advantage is that it has a clear structure and is conducive to Users know their current location very clearly. But on the web...
Statement:
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