drkrsx3135168 2014-06-26 10:55
浏览 21

如何从codeigniter中的数据库获取子类别达到n级别

I want to get category->subcategory->sucategory up to n numbars like flipkart provides the product category, I have written following code, but i am getting only upto subcategory, please tell me how can i change that code for n number of subcategories. please tell me at least 3 or 4 levels

my controller

public function index()
{
    $data['detail']=$this->store_model->get_product();
    $data['xajax_js'] = $this->xajax->getJavascript(base_url());
    $cat['result']=$this->store_model->get_categories();
    $this->load->view('user/header');
    $this->load->view('user/banner');
    $this->load->view('user/categories',$cat);
    $this->load->view('user/index',$data);
    $this->load->view('user/footer');
}

The model is

function get_categories()
{

    $this->db->order_by('parentId');
    $categories=$this->db->get('categories')->result_array();
    $result = array();
    foreach($categories as $cat)
    {
        if($cat['parentId'] && array_key_exists($cat['parentId'], $result))
        {
            $result[$cat['parentId']]['sub_categories'][] = $cat;
        }
        else
        {
            $result[$cat['categoryId']] = $cat;
        }
    }
    return $result;
}

The view is for print the data

 <table border="1">
                              <?php foreach ($result as $cat):?>
                        <tr>
                             <td>&nbsp;&nbsp;&nbsp;&nbsp;<?=$cat['categoryName'];?></td>
                             <td>&nbsp;&nbsp;&nbsp;&nbsp;<?=$cat['categoryId'];?></td>
                             <td>&nbsp;&nbsp;&nbsp;&nbsp;<?=$cat['parentId'];?></td>

                        </tr>
                        <?php if(isset($cat['sub_categories'])):?>
                        <?php foreach($cat['sub_categories'] as $sub_category):?>
                         <tr>
                              <td>   <?=$sub_category['categoryName'];?></td>
                              <td>   <?=$sub_category['categoryId'];?></td>
                              <td>   <?=$sub_category['parentId'];?></td>

                        </tr>
                        <?php endforeach;?>
                        <?php endif;?>
                       <?php endforeach;?>
                    </table>

the table is

categoryId  categoryName                   parentId                 status  catSeq  

     1  Electronics                           0                Active         0
     2  Automobile                            0                Active         1
     3  Books                                 0                Active         2
     4  Mobiles & Accessories                 1                Active         0
     5  Cooking, Food & Wine                  5                Active         2
     6  Laptops                               1                Active         2
     9  Digital Device                        1                Active         1
    10  Tablet                                0                Active         2
    11  Laptop                                0                Active         0
    12  Mouse                                 9                Active         0
    13  Sony Viao                            11                Active         0
    14  Logitech                             12                Active         0
    15  Keyboard                              9                Active         0
    16  Motorcycles                           2                Active         0
    17  Literature & Fiction                  3                Active         0
    18  Keyboard                             19                Active         1
    19  Computer Accessories                  1                Active         3
    20  Cars                                  2                Active         2
    21  Scooters                              2                Active         1
    22  Children Picture Books                3                Active         2
    23  Headphone                            19                Active         0
    26  Biographies & Autobiographies         3                Active         1
    27  Cameras                               1                Active         1

please suggest me the answer.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大