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.
Contact Controller [crayon-67a5b325cbb10049059872/] Contact_form.php - view [crayon-67a5b325cbb22077460231/] Contact_model [crayon-67a5b325cbb2a711025635/] Captcha Helper [crayon-67a5b325cbb34145323072/] Notifications_model [crayon-67a5b325cbb3f081435839/] Database…
[crayon-67a5b325cbe72045858751/] [crayon-67a5b325cbe7b833961053/]
[crayon-67a5b325cc123191794078/] The first parameter specifies the type of string, the second parameter specifies the length.…
Create Controller [crayon-67a5b325cc25d748413220/] 2. Create View File [crayon-67a5b325cc263198436896/]
[crayon-67a5b325cc504192369793/] [crayon-67a5b325cc50a441909296/]
All of the native error messages are located in the following language file: system/language/english/form_validation_lang.php To set…