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...