Php 如何从codeigniter中的数据库中获取高达n级的子类别
我想获得类别->子类别->成功,最多n个数字,如flipkart提供的产品类别, 我已经写了以下代码,但我只得到了子类别, 请告诉我如何更改n个子类别的代码。 请告诉我至少3或4个级别 我的控制器Php 如何从codeigniter中的数据库中获取高达n级的子类别,php,codeigniter,Php,Codeigniter,我想获得类别->子类别->成功,最多n个数字,如flipkart提供的产品类别, 我已经写了以下代码,但我只得到了子类别, 请告诉我如何更改n个子类别的代码。 请告诉我至少3或4个级别 我的控制器 public function index() { $data['detail']=$this->store_model->get_product(); $data['xajax_js'] = $this->xajax->getJavascript(base_u
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>
请告诉我答案。您是在请求MySQL查询,还是子类别的CSS-HTML?请确认,stackoverflow是一个问答网站。您需要具体说明您面临的问题,而不是要求用户提供代码。@nightgaunt我问的是MySQL解决方案,我知道它应该如何以CSS和HTML打印。给我MySQL或Codeigniter的解决方案。
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