PHP infinite classification menu example program_PHP tutorial

WBOY
Release: 2016-07-13 10:48:28
Original
861 people have browsed it

When doing PHP programs, we often encounter programming that designs multi-level menus. For example, our common three-level menus first display the first-level classification of products, then display the second-level classification, and finally display the products. This way It forms a three-level structure. If there is no good classification menu support in the background, it will be very troublesome to change the data.


I was working on a content management project recently, and the client insisted on a multi-level classification. In fact, many open source backends have unlimited levels of classification, such as the backend of bKjia.c0m, and the data of these categories are only saved to a data table. Here, we just use the source code to make the association.

The associated data is not complicated. In fact, an excellent backend design should have unlimited categories. In this way, when doing secondary development, there is no need to program separately. As long as the functions are the same, a category can be added to the backend. That's it, this achieves function sharing.

A simpler infinite-level category menu code. I will simply implement the above principle. The key to this program is that the design of the data table is very unique. There is no need for recursion. The menu can be listed with a simple SQL statement. , take a look at how this data table is designed:

The database fields are roughly as follows:
————————————————————————————
id number

fid parent category number

name category name

path classification path, with id as the node, forming a string like ,1,2,3,4,
———————————————————————————-

You can assume the following data

id fid name path
—————————————————-
1 0 Category 1 ,1,

2 0 Category 2 ,2,

3 1 Category 1-1,1,3,

4 1 Category 1-2,1,4,

5 2 Category 2-1,2,5,

6 4 Category 1-2-1 ,1,4,6,
—————————————————-
All the implemented PHP codes are encapsulated in classes. It is not necessary, but I also want to be familiar with OO, haha! , let’s take a look at the page code:

Page: menu.php Author: www.bKjia.c0m
The code is as follows
 代码如下 复制代码

/**************************************
页面:menu.php
作者:www.bKjia.c0m
功能:定义数据库操作及生成菜单列表类

**************************************/
class menu{
//创建构造函数,作用:数据库连接并选择相应数据库
public function __construct(){
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "7529639";
$dbname = "menu";
mysql_connect($dbhost,$dbuser,$dbpassword) or die("error!");
mysql_query("SET NAMES 'GBK'");
mysql_select_db($dbname);
}

//执行SQL语句函数
private function query($sql){
return mysql_query($sql);
}

//取得bKjia.c0m结果集数组函数
private function loop_query($result){
return mysql_fetch_array($result);
}
//列出菜单列表函数
public function menulist(){
$sql="select * from list order by path";
$result=$this->query($sql);
while($rows=$this->loop_query($result)){
if(substr_count($rows['path'],',')>2){
for($i=0;$i<(substr_count($rows['path'],',')-2);$i++)
echo ' ';
}
echo $rows['name'].'
';
}
}

//创建析构函数,作用:关闭数据库连接
public function __destruct(){
return mysql_close();
}
}
$db=new menu();//生成实例
$db->menulist();//调用方法生成菜单
?>

Copy code

/**************************************

Function: Define database operations and generate menu list classes<🎜> <🎜>*******************************************/<🎜> class menu{<🎜> //Create a constructor, its function is to connect to the database and select the corresponding database<🎜> public function __construct(){<🎜> $dbhost = "localhost";<🎜> $dbuser = "root";<🎜> $dbpassword = "7529639";<🎜> $dbname = "menu";<🎜> mysql_connect($dbhost,$dbuser,$dbpassword) or die("error!");<🎜> mysql_query("SET NAMES 'GBK'");<🎜> mysql_select_db($dbname);<🎜> }<🎜> <🎜>//Execute SQL statement function<🎜> private function query($sql){<🎜> return mysql_query($sql);<🎜> }<🎜> <🎜>//Get bKjia.c0m result set array function<🎜> private function loop_query($result){<🎜> return mysql_fetch_array($result);<🎜> }<🎜> //List menu list function<🎜> public function menulist(){<🎜> $sql="select * from list order by path";<🎜> $result=$this->query($sql); while($rows=$this->loop_query($result)){ if(substr_count($rows['path'],',')>2){ for($i=0;$i<(substr_count($rows['path'],',')-2);$i++)<🎜> echo ' ';<🎜> }<🎜> echo $rows['name'].'
'; } } //Create a destructor, its function is to close the database connection public function __destruct(){ return mysql_close(); } } $db=new menu();//Generate instance $db->menulist();//Call method to generate menu ?> Tips: This generates an unlimited classification menu. Of course, it is not only applied to the menu, but also can be applied to the classification of products and the classification of regions. The addition, deletion, modification and checking in the background need to be done by yourself. Go and write it down Original text from: 04ie.com

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632776.htmlTechArticleWhen doing PHP programs, we often encounter programming to design multi-level menus, such as our common three-level menus , first display the first-level classification of the product, then display the second-level classification, and finally display...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!