Multi Level Nested Category System in Codeigniter and MySql

Last updated on August 19th, 2017 by Paritosh Pandey


Hello, today we are going to discuss on about multi level category system in codeigniter which is maybe very common module or feature of 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

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 is.

1. creating database structure for category table

multi-level-category-system

SQL code for category table


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 everything right as I mentioned in this brief tutorial, then after running the controller “categories()” should produce the following result-

multi-level-category-system

in controller “categories()  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 over you friends.

You may also like

3 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?

Leave a Reply

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