Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Ref : https://www.myprogrammingtutorials.com/multi-level-nested-category-system-codeigniter-mysql.html
- CREATE TABLE `categories` (
- `cat_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
- `cat_name` varchar(50) COLLATE latin1_general_ci NOT NULL,
- `parent_id` int(10) unsigned NOT NULL DEFAULT '0',
- PRIMARY KEY (`cat_id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
- Model Function
- public function get_categories(){
- $this->db->select('*');
- $this->db->from('categories');
- $this->db->where('parent_id', 0);
- $parent = $this->db->get();
- $categories = $parent->result();
- $i=0;
- foreach($categories as $p_cat){
- $categories[$i]->sub = $this->sub_categories($p_cat->cat_id);
- $i++;
- }
- return $categories;
- }
- public function sub_categories($id){
- $this->db->select('*');
- $this->db->from('categories');
- $this->db->where('parent_id', $id);
- $child = $this->db->get();
- $categories = $child->result();
- $i=0;
- foreach($categories as $p_cat){
- $categories[$i]->sub = $this->sub_categories($p_cat->cat_id);
- $i++;
- }
- return $categories;
- }
Add Comment
Please, Sign In to add comment