php - how to Get the subcategories upto n levels from database in codeigniter -
i want category->subcategory->sucategory n numbars flipkart provides product category, have written next code, getting upto subcategory, please tell me how can alter code n number of subcategories. please tell me @ to the lowest degree 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
function get_categories() { $this->db->order_by('parentid'); $categories=$this->db->get('categories')->result_array(); $result = array(); foreach($categories $cat) { if($cat['parentid'] && array_key_exists($cat['parentid'], $result)) { $result[$cat['parentid']]['sub_categories'][] = $cat; } else { $result[$cat['categoryid']] = $cat; } } homecoming $result; } the view print data
<table border="1"> <?php foreach ($result $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'] $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, nutrient & 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 image books 3 active 2 23 headphone 19 active 0 26 biographies & autobiographies 3 active 1 27 cameras 1 active 1 please suggest me answer.
php codeigniter
No comments:
Post a Comment