Use " function recursion " to implement a dynamic tree menu based on PHP and MySQL _php tutorial

Source: Internet
Author: User
Tags php and mysql
Tree menu has a very wide range of applications in many desktop applications, the main advantage is that the structure is clear, so that users can very clearly know the location of their current. But the application of the tree menu on the Web because there is no ideal ready-made components can be used directly, so in general, programmers mainly through JavaScript to implement some simple tree structure menu, but these menus are often in advance to set the menu items, as well as the various menu items in the hierarchical relationship, is not conducive to expansion, once another menu structure needs to be rewritten, so it is not very convenient to use.
Through the study of function recursion, I found that this kind of tree menu can be changed dynamically by recursive function, so that the display of the tree menu is not limited by the series. Here is the processing code of a dynamic tree menu I wrote with Php,mysql,javascript, and if you are interested, come and see how I realized it:)
First, we need a database in which 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 indexed
PARENT_ID is used to save the ID number of the previous menu, or 0 if it is a first-level menu
Name is the menu, which is the menu content to display on the page
URL if a menu is an end-level menu, you need to specify the URL address of the connection, which is the other non-end menu that is used to hold this address, the field is empty
Well, the database is there, you can add some records, here are some of the records I used when I did the test:
INSERT into Menu VALUES (' 1 ', ' 0 ', ' Personnel Management ', ');
INSERT into Menu VALUES (' 2 ', ' 0 ', ' Communication 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 ', ' contacts ', ');
INSERT into Menu VALUES (' 6 ', ' 2 ', ' web conferencing ', ');
INSERT into Menu VALUES (' 7 ', ' 3 ', ' Add file ', ' http://localhost/personal/add_achive.php ');
INSERT into Menu VALUES (' 8 ', ' 3 ', ' Query file ', ' 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 ', ' New communication record ', ' http://localhost/communication/add_address.php ');

http://www.bkjia.com/PHPjc/445580.html www.bkjia.com true http://www.bkjia.com/PHPjc/445580.html techarticle Tree menu has a very wide range of applications in many desktop applications, the main advantage is that the structure is clear, so that users can very clearly know the location of their current. But on the web ...

  • 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.