Php回收下拉菜单帮助nedded



我需要显示一个下拉列表,其中包含我编写此代码来执行此操作的课程。 但我的问题是它很好地显示了课程类别和子类别。 但是我需要将类别包装到选项组标签中。我厌倦了这样做,但它失败了。

这是我的表结构

id  parent_id   course_name
 1       0             UG
 2       0             PG
 3       1             Bsc
 4       3             Computer science
 5       3             Chemisty
 6       2             Msc
 7       6             Computer science
 8       6             Chemistry  

这是递归的 php 代码

function load_course_category(){
        $this->db->select('*');
        $this->db->from('courses_category');
        $this->db->where('parent_id','0');      
        $this->db->order_by("id", "desc"); 
        $query = $this->db->get();
        foreach($query->result() as $row) {     
            $this->db->where('parent_id', $row->id);
            $this->db->from('courses_category');
            $count = $this->db->count_all_results();            
            if($count>0){
                    echo '<optiongroup label="'.$row->course_name.'">';     
            }else{
                    echo '<optiongroup label="'.$row->course_name.'"></optiongroup>';       
            }

            $this->get_children($row->id);
        }
    }
function get_children($parent, $level = 1){
        $this->db->select('*');
        $this->db->from('courses_category');
        $this->db->where('parent_id',$parent);      
        $this->db->order_by("id", "desc"); 
        $query = $this->db->get();
        if($query->num_rows()>0){
                foreach($query->result() as $row) {
                    $this->db->where('parent_id', $row->id);                
                    $this->db->from('courses_category');
                    $count = $this->db->count_all_results();
                    if($count>0){   
                        echo '<optiongroup label="'.$row->course_name.'">';         
                    }else{
                        echo '<optiongroup label="'.$row->course_name.'"></optiongroup>';           
                    }
                    $this->get_children($row->id, $level+1);
                }
        }
}

此处的选项组未正确关闭。 谁能帮我纠正这个问题?

谢谢。

无论

IF语句的结果如何,您都在打开新的optiongroup,但您只是if ($count>0)关闭它

最新更新