Introduction to the use of recursive function return values in PHP (ecshop unlimited classification) _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Introduction to the use of recursive function return values in PHP (ecshop unlimited classification ). An example of ecshop unlimited classification is used to introduce the return value of recursive functions in php. When indexing products in the secondary development of ecshop, you need to obtain an example of ecshop unlimited classification based on the Category id to introduce the recursive function return value in php.

During secondary development of ecshop, you must obtain the top-level Category id based on the Category id. The first reaction is to use recursion, so the recursive function is written as follows:

The code is 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 does not get the return value? After checking for a long time, no error was found. it seems that the brain is broken. When I asked Shen Shui (a good-hearted netizen) today, he helped me solve the problem and made the following changes:

The code is 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']); // write a return statement to get the function return value.
}
Else
{
Return $ res [0] ['cat _ id'];
}}
Else
{
Return 1;
}
}

The function is written internally. even if it is returned, it is only returned to the position of the internal function. Therefore, there is a main function outside, and you must return it again.

Bytes. In the secondary development of ecshop, you need to obtain the product category index based on the category id...

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.