


Use function recursion to implement dynamic... 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 upper-level menu, if it is a first-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 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 the database is there, you can add some records. Here are some records I used when doing tests:
INSERT INTO menu VALUES ( '1', '0', '人事管理', ''); INSERT INTO menu VALUES ( '2', '0', '通讯交流', ''); INSERT INTO menu VALUES ( '3', '1', '档案管理', ''); INSERT INTO menu VALUES ( '4', '1', '考勤管理', 'http://localhost/personal/attendance.php'); INSERT INTO menu VALUES ( '5', '2', '通讯录', ''); INSERT INTO menu VALUES ( '6', '2', '网络会议', ''); INSERT INTO menu VALUES ( '7', '3', '新增档案', 'http://localhost/personal/add_achive.php'); INSERT INTO menu VALUES ( '8', '3', '查询档案', 'http://localhost/personal/search_archive.php'); INSERT INTO menu VALUES ( '9', '3', '删除档案', 'http://localhost/personal/delete_archive.php'); INSERT INTO menu VALUES ( '10', '5', '新增通讯记录', 'http://localhost/communication/add_address.php'); INSERT INTO menu VALUES ( '11', '5', '查询通讯记录', 'http://localhost/communication/search_address.php'); INSERT INTO menu VALUES ( '12', '5', '删除通讯记录', 'http://localhost/communication/delete_address.php'); INSERT INTO menu VALUES ( '13', '6', '召开会议', 'http://localhost/communication/convence_meeting.php'); INSERT INTO menu VALUES ( '14', '6', '会议查询', 'http://localhost/communication/search_meeting.ph');
When adding records, be sure to pay attention to the parent_id of the non-first-level menu Be sure to specify it as the ID number of the upper-level menu, otherwise your menu will not be displayed:)
Okay! With the database, the following is to read the menu from the database through php and JavaScript and display it:)
1. JavaScript script:
function ShowMenu(MenuID) { if(MenuID.style.display=="none") { MenuID.style.display=""; } else { MenuID.style.display="none"; } }
This script is very simple, it is used to respond to clicking on a certain menu. of events.
2. CSS file:
TD { FONT-FAMILY: "Verdana", "宋体"; FONT-SIZE: 12px; LINE-HEIGHT: 130%; letter-spacing:1px } A:link { COLOR: #990000; FONT-FAMILY: "Verdana", "宋体"; FONT-SIZE: 12px; TEXT-DECORATION: none; letter-spacing:1px } A:visited { COLOR: #990000; FONT-FAMILY: "Verdana", "宋体"; FONT-SIZE: 12px; TEXT-DECORATION: none; letter-spacing:1px } A:active { COLOR: #990000; FONT-FAMILY: "Verdana", "宋体"; FONT-SIZE: 12px; TEXT-DECORATION: none; letter-spacing:1px } A:hover { COLOR: #ff0000; FONT-FAMILY: "Verdana", "宋体"; FONT-SIZE: 12px; TEXT-DECORATION: underline; letter-spacing:1px } .Menu { COLOR:#000000; FONT-FAMILY: "Verdana", "宋体"; FONT-SIZE: 12px; CURSOR: hand }
defines some basic style information, such as fonts, colors, hyperlink styles, etc. If you want to change the style, just modify it here!
3. Below is my php page!
//Basic variable settings
$GLOBALS["ID"] =1; //Used to track the ID number of the drop-down menu
$layer=1; //Used to track the level of the current menu
//Connect to the database
$Con=mysql_connect("localhost","root","");
mysql_select_db("work");
//Extract the first-level menu
$sql="select * from menu where parent_id=0";
$result=mysql_query($sql,$Con);
//If the first-level menu exists, start the display of the menu
if(mysql_num_rows($result)>0) ShowTreeMenu($Con,$result,$layer,$ ID);
//============================================ ===
//Show tree menu function ShowTreeMenu($con,$result,$layer)
//$con: database connection
//$result: the menu record set that needs to be displayed
//layer: what needs to be displayed Levels of menu
//============================================== ==
function ShowTreeMenu($Con,$result,$layer)
{
//Get the number of menu items that need to be displayed
$numrows=mysql_num_rows($result);
//Start to display the menu, each submenu Use a table to represent
echo " ";
for($rows=0;$rows<$numrows;$rows++)
{
//Import the contents of the current menu item into the array
$menu=mysql_fetch_array($result) ;
//Extract the submenu record set of menu items
$sql="select * from menu parent_id=$menu[id]";
$result_sub=mysql_query($sql,$Con);
echo " " ;
//If the menu item has a submenu, add a JavaScript onClick statement
if(mysql_num_rows($result_sub)>0)
{
echo " ";
echo " ";
}
else
{
echo " ";
echo " ";
}
//If the menu item has no submenu and a hyperlink address is specified, it is specified as a hyperlink,
//Otherwise only the menu name is displayed
if($menu[url]! ="")
echo "$menu[name]";
else
echo $menu[name];
echo "
";
//If the menu item has a submenu, display the submenu
if(mysql_num_rows($result_sub)>0)
{
//Specify the ID and style of the submenu to correspond to the onClick statement
echo " ";
echo " ";
echo " ";
//Will The series increases by 1
$layer++;
//Recursively call the ShowTreeMenu() function to generate a submenu
ShowTreeMenu($Con,$result_sub,$layer);
//The submenu processing is completed and returns to the previous layer of the recursion. Decrease the series by 1
$layer--;
echo "
";
}
//Continue to display the next menu item
}
echo " ";
}
?>
in the php page above Inside, I defined a function ShowTreeMenu(). Through the call of this function, each menu item will be called recursively from the database and displayed on the page:)
Want to get more related content 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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

CMS stands for Content Management System. It is a software application or platform that enables users to create, manage, and modify digital content without requiring advanced technical knowledge. CMS allows users to easily create and organize content

Arrays are linear data structures used to process data in programming. Sometimes when we are processing arrays we need to add new elements to the existing array. In this article, we will discuss several ways to add elements to the end of an array in PHP, with code examples, output, and time and space complexity analysis for each method. Here are the different ways to add elements to an array: Use square brackets [] In PHP, the way to add elements to the end of an array is to use square brackets []. This syntax only works in cases where we want to add only a single element. The following is the syntax: $array[] = value; Example
