首页 > 解决方案 > 选择一个选项的递归选择php codeigniter

问题描述

我有递归函数,它在我的选择下拉列表中填充数据。

我如何传递一个值,如果它与下拉列表中的任何值匹配,就会被选中?

这是我的模型上的功能:

function get_all_cat($parent,$level=0,$s){
        $ui = '';
        $this->db->order_by($this->id, $this->order);
        $this->db->where('parent',$parent);
        $query=$this->db->get($this->table)->result();
        
        foreach ($query as $qu) {
        if($s==$qu->id){
        $y = 'selected';
        }
        $repeat = str_repeat('--', $level);
        
        $ui .= '<option value="'. $qu->id .'" '.$y.' >' .$repeat. $qu->title.'</option>';
        
        $new_level = $level+1;
        $ui .= '' .$this->get_all_cat($qu->id,$new_level) .'';
        }
        return $ui;
}

这是我在视图中的选择选项:

<div class="form-group">
    <select name="category" class="form-control chosen-select">
        <option value="">Any Category</option>
        <?php
        $cat =   $this->Dbc_categories_model->get_all_cat(0,0,$this-session->userdata('clicked_id');
        echo $cat;
        ?>
    </select>
</div>

标签: phpcodeigniterrecursionselectdropdown

解决方案


我捡到的几张纸条……

1.) 永远不要使用像 $s => 容易迷路的变量名称,并且该名称不会告诉您变量包含或做什么

2.)您没有将选定的值传递给隐蔽函数(缺少第三个参数)

否则看起来这是工作代码。


推荐阅读