


Summary of methods to achieve unlimited classification in PHP+Mysql, phpmysql_PHP tutorial
A summary of methods to implement unlimited classification in PHP+Mysql. phpmysql
Infinite classification is an old topic. Let’s take a look at how to implement PHP combined with Mysql.
The first method
This method is very common and traditional. Let’s look at the table structure first
Table:category
id int primary key, auto-increment
name varchar category name
pid int parent class id, default 0
The pid of the top category is 0 by default. When we want to take out the sub-category tree of a certain category, the basic idea is recursion. Of course, due to efficiency issues, it is not recommended to query the database for every recursion. The usual approach is to first retrieve all categories and save them in a PHP array. , and then process it, and finally cache the results to improve the efficiency of the next request.
First build an original array, which can be pulled directly from the database:
Copy code The code is as follows:
$categories = array(
Array('id'=>1,'name'=>'Computer','pid'=>0),
array('id'=>2,'name'=>'mobile phone','pid'=>0),
array('id'=>3,'name'=>'Notebook','pid'=>1),
array('id'=>4,'name'=>'Desktop','pid'=>1),
Array('id'=>5,'name'=>'Smartphone','pid'=>2),
array('id'=>6,'name'=>'Function machine','pid'=>2),
array('id'=>7,'name'=>'Ultrabook','pid'=>3),
Array('id'=>8,'name'=>'Game Note','pid'=>3),
);
The goal is to convert it into the following structure
Computer
Notebook
Ultrabook
Game notebook
Desktop
Mobile phone
Smartphone
Function phone
If represented by an array, you can add a children key to store its subcategories:
Copy code The code is as follows:
array(
//1 corresponds to the id, which is convenient for direct reading
1 => array(
'id'=>1,
'name'=>'Computer',
'pid'=>0,
children=>array(
&array(
'id'=>3,
'name'=>'Notebook',
'pid'=>1,
'children'=>array(
//Omit here
)
),
&array(
'id'=>4,
'name'=>'Desktop',
'pid'=>1,
'children'=>array(
//Omit here
)
),
)
),
//Other categories are omitted
)
Processing process:
Copy code The code is as follows:
$tree = array();
//The first step is to use the classification id as the array key and create the children unit
foreach($categories as $category){
$tree[$category['id']] = $category;
$tree[$category['id']]['children'] = array();
}
//The second part uses references to add each category to the children array of the parent class, so that a tree structure can be formed in one traversal.
foreach ($tree as $k=>$item) {
If ($item['pid'] != 0) {
$tree[$item['pid']]['children'][] = &$tree[$k];
}
}
print_r($tree);
The printing results are as follows:
Copy code The code is as follows:
Array
(
[1] => Array
(
[id] => 1
[Name] = & gt; computer
[pid] =>
[children] => Array
(
(
Since
[pid] => 1
[children] => Array
(
(
[name] => Ultrabook
[pid] => 3
[children] => Array
(
)
)
(
[name] => Game Notebook
[pid] => 3
[children] => Array
(
)
)
)
)
[1] = & gt; Array
(
[name] => Desktop
[pid] => 1
[children] => Array
(
)
)
)
)
[2] => Array
(
[id] => 2
[Name] = & gt; Mobile phone
[pid] =>
[children] => Array
(
(
[name] => Smartphone
[pid] => 2
[children] => Array
(
)
)
[1] = & gt; Array
(
Since
[name] => Function phone
[pid] => 2
[children] => Array
(
)
)
)
)
[3] => Array
(
[id] => 3
[Name] = & gt; notebook
[pid] => 1
[children] => Array
(
Since
(
[name] => Ultrabook
[pid] => 3
[children] => Array
(
)
)
(
[name] => Game Notebook
[pid] => 3
[children] => Array
(
)
)
)
)
[4] => Array
(
[id] => 4
[name] =>
[pid] => 1
[children] => Array
(
)
)
[5] => Array
(
[id] => 5
[Name] = & gt; smart machine
[pid] => 2
[children] => Array
(
)
)
[6] => Array
(
[id] => 6
[Name] = & gt; function phone
[pid] => 2
[children] => Array
(
)
)
[7] => Array
(
[id] => 7
[name] =>
[pid] =>
[children] => Array
(
)
)
[8] => Array
(
[id] => 8
[Name] = & gt; game book
[pid] =>
[children] => Array
(
)
)
)
Disadvantages: Using PHP for processing, if the number of categories is huge, the efficiency will also be reduced.
The second method
This method is to add a path field to the table field:Table:category
id int primary key, auto-increment
name varchar category name
pid int parent class id, default 0
path varchar path
Sample data:
1 Computer 0 0
2 mobile phone 0 0
3 to
4 Ultrabook 3 0-1-3
5 Game notebook 3 0-1-3
The path field records the path from the root category to the upper-level parent category, represented by id+'-'.
In this way, assuming we want to query all descendant categories under the computer, we only need one sql statement:
select id,name,path from category where path like (select concat(path,'-',id,'%') as path from category where id=1);
Result:
+----+-----------+-------+
+----+-----------+-------+
| 3 | Notebook | 0-1 |
| 4 | Ultrabook | 0-1-3 |
| 5 | Game Book | 0-1-3 |
+----+-----------+-------+
This method has also been adopted by many people. I summarized it as follows:
Advantages: easy query, high efficiency, path field can be indexed.
Disadvantages: Updating node relationships is troublesome and requires updating the path fields of all descendants.
The above is the entire content of this article, two methods, which one do you prefer? Hope you all like it.
http://www.bkjia.com/PHPjc/963127.html

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.