在php和Codeigniter中安排类别和子类别

时间:2015-09-30 09:16:29

标签: php codeigniter

这里我有我的数据库表,我的表名是我将数据类别和子类别存储在类别列中的类别 Here i have my database table and my table name is category where I store my data category and subcategory in category column

我想在ul li中获取此格式的数据这个代码我试图安排,但我不是完全像这样获取数据请帮我安排我的代码这种格式。 i want to fetch this data like this format in ul li  for this code i have trying to arrange this format but am not fetch data perfectly like this format please help me to arrange my code this format.

2 个答案:

答案 0 :(得分:1)

您可以尝试这样

   //to select categories
        $this ->db-> select();
        $this->db->from('category');
        $this->db->where('pid',0);
        $query = $this->db->get();
        $categories = array();

        $i=0;
        foreach ($query->result_array() as $row) {
        //put all category names to $categories array
        $categories[$i] =$row['category'];
        $i++;
        }

 //to select relevant sub categories
        $sub_categories=array();
        foreach ($categories as $category) {
            $this ->db-> select();
            $this->db->from('category');
            $this->db->like('category', $category, 'after'); 
            $query = $this->db->get();

             $j=0;
                foreach ($query->result_array() as $row) {
                //put all sub categories names to $sub_categories array
                $sub_categories[$category][$j] =$row['category'];
                $j++;
                }

        }

        $return_data['categories']=$categories;
        $return_data['sub_categories']=$sub_categories;

        return $return_data;

答案 1 :(得分:0)

此代码对于获取第n 级别的类别,子类别数组

非常有用

<强>控制器:

$this->load->model('getmenu_model');

function get_menu() {
    $ci = & get_instance();
    $ci->load->model("getmenu_model");
    $menu['menu'] = $ci->getmenu_model->get_menu();
    $echo = echoMenu($menu['menu']);

    //$echo.= "<li><a href=" . site_url('loose_diamond').">Loose Diamonds</a></li>";
    $menu['print_menu']=$echo; 
    print_r($menu);
    //return $menu;

}

function echoMenu($arr) {

    $ci = & get_instance();
    $echo = "";
    //$echo.="<ul>";

    foreach ($arr as $subArr) {
        if (!empty($subArr['sub_menu'])) {
            $echo.= "<li>";
            $echo.= "<a href=" . site_url('product_display/index') . "/" . $subArr['id'] . ">" . $subArr['name'] . "</a>";
            if ($subArr['sub_menu']) {
                $echo.= "<ul>";
                $echo .=echoMenu($subArr['sub_menu']);
                $echo.= "</ul>";
            }
            $echo.= "</li>";
        } else {
            $echo.= "<li><a href=" . site_url('product_display/index') . "/" . $subArr['id'] . ">" . $subArr['name'] . "</a></li>";
        }
    }

    //$echo.= "</ul>";       
    return $echo;
}

<强>模型:

public function get_menu() {
       $this->db->select('id,name,parent_id');           
        $menu = $this->db->get('category')->result_array();
        $data=$this->menu_child($menu);
        //print_r($data);        
        return $data;
    } 
    function menu_child($menu, $parent = NULL) {
//        echo $parent."---";
       $main_menu = array_filter($menu, function($a)use($parent) {
                   return $a['parent_id'] == $parent;
               });

//       print_r($main_menu);
       if ($main_menu) {
           foreach ($main_menu as $key => $value) {
               $main_menu[$key]['sub_menu'] = $this->menu_child($menu, $value['id']);
           }
       }
//       print_r($main_menu);
       return $main_menu;
    }