Implement unlimited classification using php + mysql non-recursive method

Source: Internet
Author: User
The recursive method is the easiest way to think about using php to implement an infinite classification, but recursion occupies a lot of system resources. Therefore, we generally do not consider using recursion to implement an infinite classification using php, recursion is the easiest way to think about it, but recursion occupies system resources. Therefore, recursion is generally not considered,
This article uses non-recursive database design to implement unlimited classification
The database structure is as follows:
Id
Fid parent category ID
Class_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 class_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,
----------------------------------------------------
PHP code:
$ SQL = "SELECT * FROM tree order by path ";
$ Result = $ nbs-> Query ($ SQL );
While ($ rows = $ nbs-> fetch_array ($ result )){
If (substr_count ($ rows ['path'], ',')> 2 ){
For ($ I = 0; $ I <(substr_count ($ rows ['path'], ',')-2); $ I ++)
Echo '';
}
Echo $ rows ['class _ name'].'
';
}
?>
$ Nbs is a database operation class.

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.