PHP unlimited classification menu instance program

Source: Internet
Author: User


Recently, in a content management project, the customer has to come up with a multi-level classification. In fact, in many open-source backgrounds, there are infinite classification levels, such as the 111cn.net background, however, the data of these categories is only stored in one data table, and only associated with the source code.

The associated data is not complex. In fact, an excellent background design should be infinitely classified. In this way, you do not need to program the data separately during secondary development, as long as the function is the same, you can add a category in the background, so that the function is shared.

A simpler menu code for unlimited classification, I will simply implement the above principle. The key to this program is that the design of data tables is very distinctive and does not require recursion, you can use a simple SQL statement to list the menu and see how the data table is designed:

The database fields are roughly as follows:
----------------------------
Id

Fid parent category ID

Name category name

Path classifier path, which uses id as a node to form a string similar to, 1, 2, 3, and 4.
----------------------------

The following data can be assumed:

Id fid name path
------------------
1 0 Category 1, 1,

2 0 Category 2, 2,

3 1 classification 1-1, 1, 3,

4 1 classification 1-2, 1, 4,

5 2 categories 2-1, 2, 5,

6 4 categories 1-2-1, 1, 4, 6,
------------------
All the PHP code implemented is encapsulated in a class. It is not necessary, but I want to familiarize myself with OO !, Let's take a look at the page code:

The code is as follows: Copy code

<? Php
/**************************************
Page: menu. php
Author: www.111cn.net
Function: defines database operations and generates menu lists.

**************************************/
Class menu {
// Create a constructor to connect to the database and select a 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 functions
Private function query ($ SQL ){
Return mysql_query ($ SQL );
}

// Obtain functions of the 111cn.net result set
Private function loop_query ($ result ){
Return mysql_fetch_array ($ result );
}
// List menu list functions
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']. '<br> ';
}
}

// Create a destructor to disable database connection
Public function _ destruct (){
Return mysql_close ();
}
}
$ Db = new menu (); // Generate an instance
$ Db-> menulist (); // call the method generation menu
?>

Tips: an infinitely classified menu is generated. Of course, the menu is not only applied to the menu, but also to the product category and the classification of the local and urban areas, to add, delete, modify, and query data in the background, you need to write it yourself.

Original article from: 04ie.com

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.