Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
How To Create Multi Level Nested Category Example In Codeigniter

How to Create Multi level Nested category in Codeigniter

Posted on June 10, 2018December 17, 2022 By XpertPhp No Comments on How to Create Multi level Nested category in Codeigniter

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 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-categories through the main category, At that time we are using the recursion function.

so, you can follow the below steps.

How to Create Multi level Nested category in Codeigniter

Download and install Codeigniter

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

Create a Database in table

In this step, we have to create a table in the database, so we will create a database using the below code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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;

Connect to Database

Go to the config folder and open database.php file some changes in this file like hostname, database username, database password, and database name.

See also  Laravel 7 integrate ckeditor with example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$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
);

Create Controller

In 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?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>";
    }
}
 
?>

Create a Model

In 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?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

Codeigniter, MySql Tags:Codeigniter Multilevel Menu, Codeigniter Nested Controllers, Multi Level Menu in Codeigniter, Nested Select Query in Codeigniter

Post navigation

Previous Post: How to use Hooks in Codeigniter
Next Post: Php Form Validation Example

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Angular 15 CRUD Application Example Tutorial
  • Laravel 10 Form Validation Example Tutorial
  • Angular 15 Custom Form Validation Example
  • Laravel 10 Send Email Example Tutorial

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com