A simpler infinite category menu code_PHP tutorial

WBOY
Release: 2016-07-21 15:57:29
Original
739 people have browsed it

First of all, I would like to thank terry39 for his advice. I have nothing to do on New Year's Day, so I will simply implement the principles he said. The key to this program is that the design of the data table is very unique, without recursion, relying on a simple SQL statement You can list the menu and see how this data table is designed:
The database fields are roughly as follows:
--------------------- -------------------------------------------------- ---------
id Number
fid Number
fid Parent category number
name Category name
path Category path, with id as the node, consists of something like ,1,2,3,4, Such a string
--------------------------------------------- ---------------------------------------

It can be assumed that the following Data

id fid name name path
------------------------------------------------ ----------------
1 0 0 Category 1 ,1,
2 Category 2 ,2,
3 1 Category 1-1 , ,1,3,
4 1 Category 1-2 ,1,4,
5 Category 2-1 ,2,5,
6 Category 1-2-1 , ,1,4,6,
- -------------------------------------------------- -
To be lazy this time, I only use one page. Fortunately, the code is not long and all the codes are encapsulated in classes (not necessary, but I also want to get familiar with OO, haha!). Let’s take a look at the page code:

Copy code
The code is as follows:


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

Page: menu.php
Author: Hui Boss
Function: Define database operations and generate menu list classes

****************************** ***********/
class menu{

//Create constructor, function: database connection and select the corresponding database
public function __construct(){
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "7529639"; > 🎜> 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); 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 ' ;                                                                                      /Create a destructor, function: close the database connection
public function __destruct(){
return mysql_close();
}
}
$db=new menu();//Generate Example
$db->menulist();//Call method to generate menu
?>








http://www.bkjia.com/PHPjc/317785.html

www.bkjia.com
true
A simpler infinite category menu code_PHP tutorialhttp: //www.bkjia.com/PHPjc/317785.htmlTechArticle

First of all, I would like to thank terry39 for his guidance. I have nothing to do on New Year’s Day, so I will simply implement the principles he said. , the key to this program is that the design of the data table is very unique, without recursion, according to...
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!