How to Use the class ID to obtain all sub-category items under this category for an unlimited database category?

Source: Internet
Author: User
Cat_idcat_namepidcat_pat category ID (auto-incrementing) category name 0 (parent Id, if it is 0, It is the root category, it is auto-incrementing ID) path can you please tell me if you can give the best way, you can directly query all the following sub-category data tables by following category ID1: idcat_namepidcat_pa...

Cat_id cat_name pid cat_pat
Category id (auto-increment) category name 0 (parent Id, if it is 0, It is the root category, it is the auto-increment ID) path
Could you give me the best way to query all the sub-categories below with category ID 1?
The data table is as follows:

Id cat_name pid cat_path
1 mobile phone 0,
2 smartphones 1, 1,
3 Samsung 2, 1, 2,
4 Samsung S Series 3, 1, 2, 3,
5 Samsung M Series 3, 1, 2, 3,

Reply content:

Cat_id cat_name pid cat_pat
Category id (auto-increment) category name 0 (parent Id, if it is 0, It is the root category, it is the auto-increment ID) path
Could you give me the best way to query all the sub-categories below with category ID 1?
The data table is as follows:

Id cat_name pid cat_path
1 mobile phone 0,
2 smartphones 1, 1,
3 Samsung 2, 1, 2,
4 Samsung S Series 3, 1, 2, 3,
5 Samsung M Series 3, 1, 2, 3,

No one answered this question. Some time ago, I did some related work. Let's give it to you.
The idea is as follows: Use a stack to temporarily save unprocessed root nodes and all their subnodes, constantly extract a node input from the stack, and directly push its subnodes into the stack, until the stack is empty and the algorithm ends.
I have already rewritten the detailed comments for you. It is no problem to understand them!

$ WaitList = array (); // wait for the stack (the array is used as the stack), unprocessed id array_push ($ waitList, $ _ GET ["cid"]); // I am a node (cid) in the vertex and list the data in all subclasses $ rsList = array (); // result queue while (count ($ waitList)> 0) {// wait for a node in the stack to continue processing $ tmp = array_pop ($ waitList); // retrieve a node array_push ($ rsList, $ tmp ); // output this node $ dc = M ("DocCategory"); // find all the child nodes of this node from the data table $ dcon ['pid '] = $ tmp; $ dcs = $ dc-> where ($ dcon)-> field ('id')-> select (); foreach ($ dcs as $ value) {// press all subnodes into the wait stack array_push ($ waitList, $ value ['id']) ;}$ con ['category '] = array ('in ', $ rsList); // generates query conditions, waiting for the queue to contain all subnodes

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.