Home > php教程 > PHP开发 > Use 'function recursion' to implement dynamic tree menu based on PHP and MySQL

Use 'function recursion' to implement dynamic tree menu based on PHP and MySQL

黄舟
Release: 2016-12-14 16:14:16
Original
1257 people have browsed it

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 (m.sbmmt.com)!


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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template