In the PHP program, often encountered in the design of multi-level menu programming, such as our common level three menu, first show the first level of product classification, and then show the level two classification, and finally show the product, this constitutes a three-level structure, if there is no good category menu support in the background, then to change the data is very cumbersome.
Recently in a Content management project, the customer is hard to come up with a multilevel classification, in fact, in many open source background is an infinite classification, such as bkjia.c0m backstage, and these categories of data only saved to a data table, but only with the source code for the association.
And these related data is not complex, in fact, a good background design, should be unlimited classification, so, in the development of two times, do not have to program alone, as long as the function, the background to add a classification can be, so that the function has been shared.
A simpler infinite class menu code, I put the above principle simple implementation, the key to this program is that the data table design is very characteristic, do not have recursion, rely on a simple SQL statement can list the menu, see how this data table design:
The database fields are probably as follows:
———————————————————————————–
ID number
FID parent category number
Name Category name
Path classification paths, with IDs as nodes, consisting of similar, 1,2,3,4, such strings
———————————————————————————-
You can assume that 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 Categories 1-2-1, 1,4,6,
—————————————————-
The implementation of PHP all the code in class encapsulation, not necessary, but also want to familiarize themselves with OO, hehe! , take a look at the page code:
| The code is as follows |
Copy Code |
|
/**************************************
Page: menu.php
Author: www.bKjia.c0m
Functions: Defining Database operations and generating menu list classes
**************************************/
Class menu{
Create a constructor function: Database connection and select the appropriate 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);
}
Listing 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 ']. '
';
}
}
To create a destructor to: close a database connection
Public Function __destruct () {
return Mysql_close ();
}
}
$db =new menu ();//Generate Instance
$db->menulist ();//Invoke Method Generation menu
?>
|
Tip: This generates an infinite level of category menu, of course, not only in the menu, but also can be applied in the classification of the product and the classification of the urban area above, the background of the addition and deletion of the changes need to write their own small
Originally from: 04ie.com
http://www.bkjia.com/PHPjc/632776.html www.bkjia.com true http://www.bkjia.com/PHPjc/632776.html techarticle in the PHP program, often encountered in the design of multi-level menu programming, such as our common level three menu, first show the first level of product classification, and then show the level two classification, and finally show ...