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