如何在codeigniter中制作N级Catetgory Sub类别结构

时间:2016-07-09 09:52:31

标签: php mysql codeigniter-3

我有数据库表如下:

enter image description here

我尝试这个代码..作为列表工作,但我想将数据存储到array.i希望使菜单像电子商务和选择框也包含级别的catagory。

public function get_catagory($parent_id=0)
    { 
        $this->db->where('parent_id', $parent_id);

        $result = $this->db->get('catagory')->result();

        foreach ($result as $row){


            $i = 0;
            if ($i == 0) echo '<ul>';
            echo '<li>' .$row->catagory;

            $this->get_catagory($row->id);
            echo '</li>';
            $i++;
            if ($i > 0) echo '</ul>';

        }

    }

我需要像下面的数组..

Array
(
    [43] => Array
        (
            [id] => 43
            [catagory] => ASTM
            [parent_id] => 3
            [sub_categories] => Array
                (
                )

        )

    [44] => Array
        (
            [id] => 44
            [catagory] => DIN
            [parent_id] => 3
            [sub_categories] => Array
                (
                    [50] => Array
                        (
                            [id] => 50
                            [catagory] => BS
                            [parent_id] => 44
                            [sub_categories] => Array
                                (
                                    [52] => Array
                                        (
                                            [id] => 52
                                            [catagory] => UNE
                                            [parent_id] => 50
                                            [sub_categories] => Array
                                                (
                                                )

                                        )

                                )

                        )

                    [49] => Array
                        (
                            [id] => 49
                            [catagory] => ISO
                            [parent_id] => 44
                            [sub_categories] => Array
                                (
                                    [51] => Array
                                        (
                                            [id] => 51
                                            [catagory] => GOST
                                            [parent_id] => 49
                                            [sub_categories] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

)

2 个答案:

答案 0 :(得分:1)

将catagory结果传递给数组以下函数:

$data['catagory']=$this->db->get('catagory')->result_array();
$return['result']=$this->ordered_menu( $data['catagory'],0);
echo "<pre>";
     print_r ($return['result']);
     echo "</pre>";

创建如下函数:

function ordered_menu($array,$parent_id = 0)
    { 
        $temp_array = array();
        foreach($array as $element)
        {
            if($element['parent_id']==$parent_id)
            {
                $element['subs'] = $this->ordered_menu($array,$element['id']);
                $temp_array[] = $element;
            }
        }
        return $temp_array;
    }

答案 1 :(得分:0)

对于第N级别类别,您可以使用单个表管理,db结构将类似于id(PK),cat_name,parent_cat_id(对于主类别值将为零,否则将添加父cat id)。

例如 -

1) vehicle (main category)  
    - Bike
    - Cars
        - Maruti
            - ALTO
            - i10
        - AUDI

在上面的示例中,您可以使用递归函数检索所有子类别或父类别,此函数将运行,直到他找到类别,因此您必须使用 RECURSIVE FUNCTION