dougai2427 2015-11-24 16:58
浏览 42
已采纳

限制函数在php codeigniter中递归的次数

I want to generate a multilevel hierarchical select option to select parent or any of its child categories in codeigniter. I want to limit the hierarchy level to 3. i.e the option must show parent, its first level child and its second level child with child category indented to the right of parent. I've no problem going through all the hierarchy level. But I can't generate the select option upto a particular hierarchy level. Three levels of hierarchy is what I need in my case. Can anyone please help me.

Here is what I tried so far:

<?php
$category = $this->get_category();

function get_category() {
    $this->db->order_by('parent_id', 'DESC');
    $this->db->select('id, name, parent_id');
    return $this->db->get('tbl_job_category')->result_array();
}

/*
*$category contains the following json data obtained from DB
$category = [{"id":"20","name":"MVC","parent_id":"16"},
{"id":"16","name":"Spring","parent_id":"14"},
{"id":"12","name":"Car Driver","parent_id":"13"},
{"id":"6","name":"Electrical","parent_id":"5"},
{"id":"3","name":"Civil","parent_id":"5"},
{"id":"14","name":"java","parent_id":"2"},
{"id":"15","name":"javascript","parent_id":"2"},
{"id":"17","name":"Computer Operator","parent_id":"1"},
{"id":"2","name":"Programming","parent_id":"1"},
{"id":"4","name":"Networking","parent_id":"1"},
{"id":"11","name":"Hardware","parent_id":"1"},
{"id":"13","name":"Driver","parent_id":"0"},
{"id":"5","name":"Engineering","parent_id":"0"},
{"id":"19","name":"Economics","parent_id":"0"},
{"id":"1","name":"Information Technology","parent_id":"0"}];
*/

$category_options = $this->multilevel_select($category);

function multilevel_select($array,$parent_id = 0,$parents = array()) {
    static $i=0;
    if($parent_id==0)
    {
        foreach ($array as $element) {
            if (($element['parent_id'] != 0) && !in_array($element['parent_id'],$parents)) {
                $parents[] = $element['parent_id'];
            }
        }
    }

    $menu_html = '';
    foreach($array as $element){
        if($element['parent_id']==$parent_id){
            $menu_html .= '<option>';
            for($j=0; $j<$i; $j++) {
                $menu_html .= '&mdash;';
            }
            $menu_html .= $element['name'].'</option>';
            if(in_array($element['id'], $parents)){
                $i++;
                $menu_html .= $this->multilevel_select($array, $element['id'], $parents);
            }
        }
    }
    $i--;
    return $menu_html;
}

echo $category_options;
  • 写回答

1条回答 默认 最新

  • dqqn32019 2015-11-24 17:43
    关注

    Limiting the recursion involves three steps :

    • Initializing a counter variable passed as parameter to the recursive function.
    • Testing the counter value against the desired boundary prior to recursing
    • Passing an incremented counter value in case of recursive call.

    In your case that would be the $level variable below :

    function multilevel_select($array,$parent_id = 0,$parents = array(), $level=0) {
        // ....
                if(in_array($element['id'], $parents) && $level < 2){ // your boundary here, 2 for third nesting level from root
    
                    $i++;
                    $menu_html .= $this->multilevel_select($array, $element['id'], $parents, $level+1);
                }
    
            }
        }
        $i--;
        return $menu_html;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog