在php和Codeigniter中排列类别和子类别 [英] arrange category and subcategory in php and Codeigniter

查看:77
本文介绍了在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这个代码我试图安排,但我不是fetch数据完全像这样请帮我安排我的代码这种格式。

i want to fetch this data like this format in ul li for this code i have trying to arrange but i am not fetch data perfectly like this please help me to arrange my code this format.

推荐答案

您可以这样尝试:

   //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;

这篇关于在php和Codeigniter中排列类别和子类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆