When programming PHP programs, we often encounter multi-level menu design programming. For example, for common third-level menus, we first display the first-level category of the product, and then display the second-level category, finally, the product is displayed, which constitutes a three-level structure. If there is no good category menu support in the background, it is very troublesome to change the data.
Recently, we have been working on a content management project, and customers have to come up with a multi-level classification. In fact, many open-source backgrounds are infinitely classified, such as bKjia. c0m background, and these classified data is only saved in a data table, 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. bKjia. c0m
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 the bKjia. c0m result set number group Function
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