【问题标题】:Global variable in codeigniter helpercodeigniter 助手中的全局变量
【发布时间】:2013-05-01 04:27:18
【问题描述】:

我正在编写一个递归函数来查找数组的子元素(如果有的话)。现在我想知道一个数组进入找到它的孩子的级别。 例如

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
                            (
                            )

                    )

            )

    )

)

下面是我的递归函数,它是用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');
}

现在我想计算数组的深度,它使用相同的递归函数进入了多少级别,我试图计算它在递归函数中进入的级别,即get_children_by_par_id($parent_id)。但由于递归函数计数被初始化为其原始值。所以我需要在助手中创建一个全局变量。所以任何人都可以在这里帮助我......或者你能给我最好的主意来计算数组的深度,是的数组可以进入n级......

【问题讨论】:

    标签: php codeigniter recursion global-variables helper


    【解决方案1】:

    这帮助了我go here。我对代码做了一些曲折,更多参考您可以Visit here。你可以在codeigniter中创建一个全局变量。

    【讨论】:

      【解决方案2】:

      你可以在你的类中声明一个变量来计算级别,然后用它来计算你的函数get_children_by_par_id中的级别。

      如你在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
              }
      
          }
      

      【讨论】:

      • 它不是类,它只是一个助手,我们只有功能,没有别的......
      • 那么你需要使用配置文件创建一个全局变量,你可以在这里查看详细信息ellislab.com/codeigniter/user-guide/libraries/config.html
      • 该函数也被另一个循环调用,所以我希望循环再次初始化为0
      • 你能告诉我这个递归函数在哪里吗?你怎么称呼它?
      • 为什么要把问题复杂在自己身上,你可以像上面的代码一样创建一个库,如果你需要的话,你可以把它放在自动加载中,然后尽可能地使用它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多