简体   繁体   English

Codeigniter帮助器中的全局变量

[英]Global variable in codeigniter helper

im writing a recursive function that finds the children of an array if it has. 我正在编写一个递归函数来查找数组的子元素(如果有)。 now i want to know the level that an array enters finding its child. 现在我想知道一个数组进入寻找它的孩子的水平。 for an eg 例如

Array
(
[0] => stdClass Object
    (
        [fld_id] => 7
        [fld_value] => Color
        [fld_price] => 0.00
        [fld_attribute_id] => 2
        [fld_parent_id] => 5
        [children] => Array
            (
                [0] => stdClass Object
                    (
                        [fld_id] => 8
                        [fld_value] => Red
                        [fld_price] => 12.00
                        [fld_attribute_id] => 2
                        [fld_parent_id] => 7
                        [children] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [fld_id] => 10
                                        [fld_value] => light red
                                        [fld_price] => 20.00
                                        [fld_attribute_id] => 2
                                        [fld_parent_id] => 8
                                        [children] => Array
                                            (
                                            )

                                    )

                                [1] => stdClass Object
                                    (
                                        [fld_id] => 11
                                        [fld_value] => dark red
                                        [fld_price] => 4.00
                                        [fld_attribute_id] => 2
                                        [fld_parent_id] => 8
                                        [children] => Array
                                            (
                                                [0] => stdClass Object
                                                    (
                                                        [fld_id] => 14
                                                        [fld_value] => double_dark
                                                        [fld_price] => 3.00
                                                        [fld_attribute_id] => 2
                                                        [fld_parent_id] => 11
                                                        [children] => Array
                                                            (
                                                            )

                                                    )

                                                [1] => stdClass Object
                                                    (
                                                        [fld_id] => 15
                                                        [fld_value] => single_dark
                                                        [fld_price] => 0.00
                                                        [fld_attribute_id] => 2
                                                        [fld_parent_id] => 11
                                                        [children] => Array
                                                            (
                                                            )

                                                    )

                                            )

                                    )

                            )

                    )

                [1] => stdClass Object
                    (
                        [fld_id] => 9
                        [fld_value] => Green
                        [fld_price] => 5.00
                        [fld_attribute_id] => 2
                        [fld_parent_id] => 7
                        [children] => Array
                            (
                            )

                    )

            )

    )

)

and my recursive function is below and it is written in codeigniter helper 我的递归函数在下面,它是用codeigniter helper编写的

function get_children_by_par_id($parent_id)
{
$children = get_children($parent_id);
$return_value = array();
foreach($children->result() as $result)
{
    $result->children = get_children_by_par_id($result->fld_id);
    $return_value[]= $result;
}
return ($return_value); 
}

function get_children($id){
    $CI = get_instance();
    $CI->db->where('fld_parent_id',$id);
    return $CI->db->get('tbl_attribute_values');
}

now i want to count the depth of the array that how much level does it went inside using the same recursive function i tried to count the level it went inside in the recursive function ie get_children_by_par_id($parent_id) . 现在,我想使用相同的递归函数计算数组的深度,以了解它进入内部的级别,我试图计算在递归函数即get_children_by_par_id($parent_id)进入的级别。 but due to recursive function count is initialized to its original value. 但由于递归函数计数被初始化为其原始值。 so i need to make a global variable in the helper. 所以我需要在助手中创建一个全局变量。 so can any one help me here.... or can you give me the best idea to count the depth of the array and yes the array can go to the n level ... 所以任何人都可以在这里帮助我....还是可以给我最好的主意来计算数组的深度,是的,数组可以达到n级 ...

This helped me out go here . 这帮助我出去了这里 I did a bit of twist and turns to the code and for further references you can Visit here . 我对代码做了一些改动,可以在这里访问以获取更多参考。 you can create a global variable in codeigniter. 您可以在codeigniter中创建一个全局变量。

You can declare a variable for counting levels in your class, then use it to count the levels in your function get_children_by_par_id . 您可以在类中声明一个用于计数级别的变量,然后使用它在函数get_children_by_par_id对级别进行计数。

As you see in the test_function you can get the value of the $count variable after calling the function, then you must reset it if you need to call the function again. 如您在test_function ,可以在调用函数后获取$count变量的值,然后,如果需要再次调用该函数,则必须将其重置。

class yourClass extends somthing{
        $count = 0;
        function get_children_by_par_id($parent_id){
           $children = get_children($parent_id);
           $return_value = array();
           foreach($children->result() as $result){
               $result->children = get_children_by_par_id($result->fld_id);
               $this->count++;
               $return_value[]= $result;
           }
           return ($return_value); 
        }

        function get_children($id){
             $CI = get_instance();
             $CI->db->where('fld_parent_id',$id);
             return $CI->db->get('tbl_attribute_values');
        }
        function test_function(){
             $childs = $this->get_children_by_par_id(1);
             $childs_count = $this->count; // get the levels count
             $this->count = 0; //reset the counter
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM