Implement a dynamic tree menu based on php and MySQL

Source: Internet
Author: User
Tree menus are widely used in many desktop application systems. their important strength is to understand the structure and help the application users understand their position. However, the use of tree menus on the web is not a hypothetical ready-made component.

Tree menus are widely used in many desktop application systems. their important strength is to understand the structure and help the application users understand their position. However, the use of tree menus on the web can be directly applied because there are no imaginary ready-made components. In general, it is important for programmers to use JavaScript to implement some simple tree menus, however, these menus are usually pre-defined for each menu item and the hierarchical relationship between menu items, which is not conducive to expansion. once another menu structure is required, it is often necessary to re-compile the menu, therefore, the application is not very convenient.

After studying the function delivery, I found that this kind of tree menu can be implemented through the return function so that the display of tree menus can be dynamically changed without the limit of series. The following is the processing code of a dynamic tree menu written in php, MySQL, and JavaScript. if you are interested, let's take a look at how I implement it with me :)

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, the id is the index parent_id used to reserve the id number of the top-level menu. if it is a top-level menu, it is 0.
Name is the name of the menu, that is, the menu content to be displayed on the page.
Url if a menu is a last menu, you need to specify the url address of the connection. This field is used to retain this address. Other non-last menu, this field is blank

Now that the database is ready, you can add some records. Below are some records of the application during my testing:

Insert into menu VALUES ('1', '0', 'personnel management ','');
Insert into menu VALUES ('2', '0', 'Communication interval ','');
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', 'Communication records ','');
Insert into menu VALUES ('6', '2', 'network conferencing ','');
Insert into menu VALUES ('7', '3', 'Add files', 'http: // localhost/personal/add_achive.php ');
Insert into menu VALUES ('8', '3', 'queryfile', 'http: // localhost/personal/search_archive.php ');
Insert into menu VALUES ('9', '3', 'delete files', 'http: // localhost/personal/delete_archive.php ');
Insert into menu VALUES ('10', '5', 'Add communication record ', 'http: // localhost/communication/add_address.php ');
Insert into menu VALUES ('11', '5', 'query communication record ', http: // localhost/communication/search_address.php ');
Insert into menu VALUES ('12', '5', 'delete communication record ', http: // localhost/communication/delete_address.php ');
Insert into menu VALUES ('13', '6', 'convene a Meeting ', 'http: // localhost/communication/convence_meeting.php ');
Insert into menu VALUES ('14', '6', 'conference query', 'http: // localhost/communication/search_meeting.php ');

When adding records, be sure to note that the parent_id of a non-level menu must be specified as the ID of the parent menu, otherwise your menu will not be displayed :)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.