Ecshop Infinite Classification Recursive function return value detailed

Source: Internet
Author: User
When Ecshop two times in the development of Product Classification index, the classification ID to obtain the top-level classification ID. The first response is recursive, so the recursive function is written as follows:

function Getcattopid ($cat _id) {    if ($cat _id)    {      $res = Array ();        $sql = ' SELECT cat_id, parent_id '             . ' From '. $GLOBALS [' ECS ']->table (' category ')              . ' WHERE cat_id = '. $cat _id. ' and is_show = 1 ';        $res = $GLOBALS [' db ']->getall ($sql);        if ($res [0][' parent_id '] > 0)        {            getcattopid ($res [0][' parent_id ']);        }        else        {            return $res [0][' cat_id '];        }    }    else    {        return 1;    }}

A test program that does not get the return value? The examination for a long time did not find errors, it seems the skull is broken. Today asked the God of the gods (a good friend), he helped me to solve it, modified as follows:

function Getcattopid ($cat _id) {    if ($cat _id)    {     $res = Array ();        $sql = ' SELECT cat_id, parent_id '             . ' From '. $GLOBALS [' ECS ']->table (' category ')              . ' WHERE cat_id = '. $cat _id. ' and is_show = 1 ';        $res = $GLOBALS [' db ']->getall ($sql);        if ($res [0][' parent_id '] > 0)        {            return getcattopid ($res [0][' parent_id ']);//modify place, write a return, let the function return value        }< C11/>else        {            return $res [0][' cat_id '];        }    }    else    {        return 1;    }}

The function is written internally, and even if it returns, it just returns to the inside of the function, so there is a layer of main function outside, must return again

Related Article

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.