Categories: Snippets

Multi Level Nested Category System in Codeigniter and MySql

How to create Multi Level Category System in Codeigniter

This module is based on the recursive function concept. So let’s get started to develop multi level nested category system, carefully read below steps mentioned.

I am assuming that you are familiar with CodeIgniter’s MVC structure, so I’m not going to explain what model and controller in CI are.

1. create the database structure for categories table and insert all the data as shown in below image

 

SQL code for categories table, open SQL tab in PhpMyAdmin and run below code.

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;

2. creating model functions

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;       
    }

3. creating controller function

public function categories(){

        $this->load->model('model_categories');
 $data = $this->model_categories->get_categories();

 print_r($data);
    }

 

in controller “categories()” function the variable $data stores all the categories available in your categories table. As you can see $data is an array, you can easily parse this array into any multi level menu item or lists.

Share

Recent Posts

Sample Contact Form with validation, Captcha & Notification

Contact Controller [crayon-6638dfd22e710148086380/] Contact_form.php - view [crayon-6638dfd22e723645132563/] Contact_model [crayon-6638dfd22e730184300704/] Captcha Helper [crayon-6638dfd22e73d239155251/] Notifications_model [crayon-6638dfd22e74f744212777/] Database…

5 years ago

Delete Files and Execute Database Query remotely

[crayon-6638dfd22ecc3358414542/] [crayon-6638dfd22eccc361477908/]  

5 years ago

Random String Codeigniter

[crayon-6638dfd22ee6f571017759/] The first parameter specifies the type of string, the second parameter specifies the length.…

5 years ago

Codeigniter Ajax Form Validation Example

Create Controller [crayon-6638dfd22f015490104387/] 2. Create View File [crayon-6638dfd22f01e414964767/]  

6 years ago

Codeigniter passing 2 arguments to callback – Email validation

[crayon-6638dfd22f207931373300/] [crayon-6638dfd22f20f949084179/]  

6 years ago

Setting Error Messages

All of the native error messages are located in the following language file: system/language/english/form_validation_lang.php To set…

6 years ago