How to Create Multi level Nested category example in Codeigniter – onlinecode

How to Create Multi level Nested category example in Codeigniter – onlinecode

In this post we will give you information about How to Create Multi level Nested category example in Codeigniter – onlinecode. Hear we will give you detail about How to Create Multi level Nested category example in Codeigniter – onlinecodeAnd how to use it also give you demo for it if it is necessary.

In this article, We will explain How to Create Multi level Nested category example in Codeigniter. you can use this example in Codeigniter Multilevel Menu.

Sometimes, we need to Multilevel Nested category in Codeigniter for the create a comment system, multilevel menu, and e-commerce websites.

Here, we will use the recursion function, such as we are getting all the sub-category through the main category, At that time we are using recursion function.

so, you can follow the below steps.

Overview

Step 1: Download and install CodeigniterStep 2: Create a Database in tableStep 3: Connect to DatabaseStep 4: Create ControllerStep 5: Create a Model

Step 1: Download and install CodeigniterWe are going to install Codeigniter 3, first, we will download a fresh Codeigniter 3 version and install it in our system. so you can follow below Link for Download CodeIgniter.

Step 2: Create a Database in tableIn this step, we have to create a table in the database, so we will create a database using the below code.

CREATE TABLE 'categories' (
  'category_id' int(11) NOT NULL,
  'category_parent_id' varchar(255) NOT NULL,
  'category_name' varchar(255) NOT NULL,
  'category_created' datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table 'categories'
--

INSERT INTO 'categories' ('category_id', 'category_parent_id', 'category_name', 'category_created') VALUES
(1, '0', 'asia', '2018-10-29 12:08:16'),
(2, '1', 'india', '2018-10-29 12:08:49'),
(3, '0', 'europe', '2018-10-29 12:10:16'),
(4, '0', 'africa', '2018-10-29 12:12:38'),
(5, '1', 'shri lanka', '2018-10-29 12:13:34'),
(6, '1', 'singapore', '2018-10-29 12:14:01'),
(7, '1', 'pakistan', '2018-10-29 12:14:41'),
(8, '1', 'bangladesh', '2018-10-29 12:15:08'),
(9, '4', 'Nigeria', '2018-10-29 12:15:40'),
(10, '4', 'Kenya', '2018-10-29 12:15:54'),
(11, '3', 'France', '2018-10-29 12:16:42'),
(12, '3', 'Germany', '2018-10-29 12:44:28'),
(13, '3', 'Iceland', '2018-10-29 12:17:26'),
(14, '2', 'gujarat', '2018-10-29 12:54:23'),
(15, '14', 'surat', '2018-10-29 01:03:14'),
(16, '15', 'katargam', '2018-12-08 05:55:44'),
(19, '12', 'berlin', '2018-12-08 06:06:18'),
(21, '11', 'paris', '2018-12-08 06:20:58'),
(22, '13', 'selfoss', '2018-12-08 06:22:50');

--
-- Indexes for dumped tables
--

--
-- Indexes for table 'categories'
--
ALTER TABLE 'categories'
  ADD PRIMARY KEY ('category_id');

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table 'categories'
--
ALTER TABLE 'categories'
  MODIFY 'category_id' int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=23;
COMMIT;

Step 3: Connect to DatabaseGo to the config folder and open database.php file some changes in this file like hostname, database username, database password, and database name.

$db['default'] = array(
	'dsn'	=> '',
	'hostname' => 'localhost',
	'username' => 'root',
	'password' => '',
	'database' => 'enter here database name',
	'dbdriver' => 'mysqli',
	'dbprefix' => '',
	'pconnect' => FALSE,
	'db_debug' => (ENVIRONMENT !== 'production'),
	'cache_on' => FALSE,
	'cachedir' => '',
	'char_set' => 'utf8',
	'dbcollat' => 'utf8_general_ci',
	'swap_pre' => '',
	'encrypt' => FALSE,
	'compress' => FALSE,
	'stricton' => FALSE,
	'failover' => array(),
	'save_queries' => TRUE
);

Step 4: Create ControllerIn this step, we will create a Category.php file in the “application/controller” directory and paste the below code in this controller.application/controller/Category.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');


class Category extends CI_Controller {


    /**
    * Constructor of Controller
    *
    * @return Response
   */
    public function __construct() {
        parent::__construct();
        $this->load->model('Category_model');
    }

    public function categories(){

	    $data = $this->category_model->getcategory();
		echo "<pre>";
	    print_r($data);
		echo "</pre>";
    }
}

?>

Step 5: Create a ModelIn this step, we will create a Category_model.php file in the “application/models” directory and paste the below code in this model.application/models/Category_model.php

<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
 
class Category_model extends CI_Model {
 
    public function getcategory(){

        $this->db->select('*');
        $this->db->from('categories');
        $this->db->where('category_parent_id', 0);

        $parent = $this->db->get();
        
        $categories = $parent->result();
        $i=0;
        foreach($categories as $main_cat){

            $categories[$i]->sub = $this->sub_category($main_cat->category_id);
            $i++;
        }
        return $categories;
    }

    public function sub_category($id){

        $this->db->select('*');
        $this->db->from('categories');
        $this->db->where('category_parent_id', $id);

        $child = $this->db->get();
        $categories = $child->result();
        $i=0;
        foreach($categories as $sub_cat){

            $categories[$i]->sub = $this->sub_category($sub_cat->category_id);
            $i++;
        }
        return $categories;       
    }
 
}
 
?>

We think would you like this article, so you can click on the “Show Demo” button and you can see this demo article.

Show Demo

Please follow and like us:

Hope this code and post will helped you for implement How to Create Multi level Nested category example in Codeigniter – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

  +  3  =  4

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US