我想获得category-> subcategory-> suategory,最多n个numbars,如flipkart提供的产品类别, 我写了下面的代码,但我只得到子类别, 请告诉我如何更改n个子类别的代码。 请告诉我至少3或4级
我的控制器
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');
}
模型是
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;
}
该视图用于打印数据
<table border="1">
<?php foreach ($result as $cat):?>
<tr>
<td> <?=$cat['categoryName'];?></td>
<td> <?=$cat['categoryId'];?></td>
<td> <?=$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>
表格是
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
请告诉我答案。