Multi Level Nested Category System in Codeigniter and MySql

Last updated on by , 23 comments


Hello, today we are going to discuss on multi level category system in CodeIgniter which is a very common and essential module or feature of any e-commerce websites or any website.

Creating nested category system is not as easy as you think, but it is not impossible too. Here I am going to deliver full step by step tutorial on how you can create such kind of module using Codeigniter & 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

multi-level-category-system

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

if you are done with everything right as I mentioned in this brief tutorial, then after running the controller’s “categories()” function should produce the following result-

multi-level-category-system

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.

If you find this article helpful please don’t get shy to share this article with your friends.

You may also like



23 responses to “Multi Level Nested Category System in Codeigniter and MySql”

  1. Can says:

    Hello there
    First of all thank you for your letter, how can we foreach in a select in this usage?

  2. ashish says:

    hello admin,
    i want to display menu using this array. how can i do?
    Please reply.

    • as all the data stored in "$data" variable (see step 3.), I'm using unordered list here you can use bootstrap menu as well.

      
      function fetch_menu($data){
      
      	foreach($data as $menu){
      
      		echo "<li>".$menu->cat_name."</li>";
      
      		if(!empty($menu->sub)){
      
      			echo "<ul>";
      
      			fetch_sub_menu($menu->sub);
      
      			echo "</ul>";
      		}
      
      	}
      
      }
      
      function fetch_sub_menu($sub_menu){
      
      	foreach($sub_menu as $menu){
      
      		echo "<li>".$menu->cat_name."</li>";
      		
      		if(!empty($menu->sub)){
      
      			echo "<ul>";
      
      			fetch_sub_menu($menu->sub);
      
      			echo "</ul>";
      		}		
      
      	}
      
      }
      
      echo "<ul>";
      fetch_menu($data);
      echo "</ul>";
      
      

      please let me know whether it is working or not

  3. tarik says:

    hello, thanks for the tutorial
    my english not well but i try to ask something.
    i add link my categories and when i select a categories i get related entries (categories->sub categories..) it is ok but i need pagination that result

  4. Kuldeep Singh says:

    It is multi level but not n level. I think if we want n level approach we need a recursive function.

  5. Palani says:

    Hi,
    I need to know the naming structure and how to insert this data dynamically…

  6. Rajesh says:

    Hi,
    I need to print based on the categories from different printers. how can i do..?
    Please reply.

  7. Rushi Shukla says:

    How to Edit Delete N Level categories Subcategories code-igniter. Please Send me the Code immediately.

  8. Md. Kowsar MAhmud says:

    Thanks you bro

  9. Kjell-Arne says:

    Hi!
    Thanks for sharing this. Helped me answering a few Q about category setup.
    I’m using Codeigniter to build a rather large application, and is about to start on a custom inventory/stock management.

    One question.
    How would you go about to get the main parent category from an entry registered under the, say.. third child category in an Sql statement?

    • very simple but tricky task, try to check category’s parent_id until and unless it gets 0 (zero).

      because zero parent_id means the category doesn’t have any parent category coz it is a parent category itself

      • Kjell-Arne Neshagen says:

        Thank you!
        I’m just not sure if the best way is to do this in the code, or if there is an SQL statement to do it.

        I’ll try a bit and see what i come up with.

        Thanks again!

Leave a Reply

Your email address will not be published. Required fields are marked *