Database Unlimited Classification How to get all sub-categories under the category with the category ID, the above table structure

Source: Internet
Author: User

CAT_ID cat_name pid Cat_pat
Category ID (self-increment) category name 0 (parent ID, if 0 is the root category, the self-increment ID) path
May I ask you the great God, can give the best method, directly with the category ID 1 to query out all the following sub-categories
The data table is as follows:

ID cat_name PID Cat_path
1 Mobile 0,
2 Smartphones 1, 1,
3 Samsung 2, 1, 2,
4 Samsung S Series 3,
5 Samsung M Series 3,.

Reply content:

CAT_ID cat_name pid Cat_pat
Category ID (self-increment) category name 0 (parent ID, if 0 is the root category, the self-increment ID) path
May I ask you the great God, can give the best method, directly with the category ID 1 to query out all the following sub-categories
The data table is as follows:

ID cat_name PID Cat_path
1 Mobile 0,
2 Smartphones 1, 1,
3 Samsung 2, 1, 2,
4 Samsung S Series 3,
5 Samsung M Series 3,.

No one answered, there was a job that had been done a while ago, to give you the algorithm.
The idea is as follows: use a stack to temporarily save the unhandled root node and all its child nodes, constantly take a node input from the stack, and put its immediate child nodes, pressing onto the stack until the stack is empty and the algorithm ends.
I have rewritten the detailed comments for you, to understand that should not be a problem!

    $waitList = array();                     //等待堆栈(数组用做堆栈),未处理的id    array_push($waitList, $_GET["cid"]);     //我这是点中某个节点(cid),列出所有子类中的数据    $rsList = array();                        //结果队列    while (count($waitList) > 0) {            //等待堆栈中还有节点,继续处理        $tmp = array_pop($waitList);          //取出一个节点        array_push($rsList, $tmp);            //输出这个节点        $dc = M("DocCategory");               //从数据表中找出这个节点的所有子节点        $dcon['pid'] = $tmp;        $dcs = $dc->where($dcon)->field('id')->select();        foreach ($dcs as $value) {                //将所有子节点压入等待堆栈            array_push($waitList, $value['id']);        }    }    $con['category'] = array('in', $rsList);      //生成查询条件,等待队列中已经包含了所有的子节点
  • 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.