Menu

Codeigniter 3 is easy to learn and suitable for newbies. It is a good start for someone who wants to learn Object Oriented Programming (OOP) in Hypertext Preprocessor (PHP). After that, it is no need use Command Prompt or Terminal like other PHP frameworks. In this tutorial, I will show you how to make CRUD (Create, Read, Update, and Delete) in Codeigniter 3 + Bootstrap 3 + DataTables

Front-end tools :

Source code:

DEMO: Link

Step 1:

If you still don't have Codeigniter 3 Framework installed in your machine. Go to this tutorial: ' How to install XAMPP server / How to install Codeigniter 3

If everything is good, we start with create new Controller file in application/controllers/ directory

Example: Member.php

What is Controller?

A Controller is simply a class file that is named in a way that can be associated with a URI.

Open controller Member.php file that you already created.

Enter following code:

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

class Member extends CI_Controller {

    public function __construct()
    {

    }

    public function index()
    {

    }

    public function create()
    {

    }

    public function read($member_id)
    {

    }

    public function update($member_id)
    {

    }

    public function delete($member_id = false)
    {

    }
}

Note: Just leave function empty, we will go more further after this.

After create new controller Member.php file, now we need create new Model  Member_model.php in application/models/ directory. 

Note: Where Model_names is the name of your class. Class name must have the first letter capitalized with the rest of the name lowercase. Make sure your class extends the base Model Class.

What is models?

Models are PHP classes that are designed to work with information in your database. For example, let's say you use CodeIgniter to manage a blog. You might have a model class that contains functions to insert, update, and retrieve your blog data.

Open Member_model.php file that you already created.

Enter following code:

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

class Member_model extends CI_Model {

    public function getindex()
    {

    }
    public function createMember($data)
    {

    }
    public function getMember($member_id)
    {

    }
    public function updateMember($data, $member_id)
    {

    }
    public function deleteMember($member_id)
    {

    }
}

Note: Just leave function empty, we will go more further after this.

Done step 1, next step I will show you how to load library, helper, and database in Codeigniter 3.

Step 2:

This step I will show you how to load helper, library, and database in Codeigniter 3 .

 Enter following code inside __construct method

public function __construct()
{
   parent::__construct();
   $this->load->database();
   $this->load->helper('url');
   $this->load->library('session');
   $this->load->library('form_validation');        
   $this->load->model('Member_model');
}

The construct function lets you use things over the entire class. This way you don't have to load the model/language/settings/helper/llibrary in every method.

We did step 2. Now we go next step 3 import .sql file into phpmyadmin.

 

Step 3

This step we need create a new database using phpmyadmin.
Database name: ci-db-tutorial

Example:

After creating a new database using phpmyadmin. We need to create a new user_list table inside ci-db-tutorial

or

Create using phpmyadmin console only.

Enter following code:

[code]
CREATE TABLE `user_list` (
  `person_id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `gender` varchar(25) NOT NULL,
  `address` varchar(125) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `user_list` (`person_id`, `name`, `gender`, `address`) VALUES
(7, 'John', 'Male', 'U.S.A'),
(8, 'Greek', 'Female', 'Singapore'),
(9, 'Mike', 'Male', 'Austrilia');
[/code]

 After creating database in phpmyadmin, now we need to connect codeigniter 3 to database. Open database.php file in application/config/

 You will see default the value like this:

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
	'dsn'	=> '',
	'hostname' => 'localhost',
	'username' => '',
	'password' => '',
	'database' => '',
	'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
);

Enter following config:

Enter username, password (XAMPP server by default no password), database (that we already created).

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
	'dsn'	=> '',
	'hostname' => 'localhost',
	'username' => 'root',
	'password' => '',
	'database' => 'ci-db-tutorial',
	'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
);

After done edit, now save it.

Done step 3, now we go to step 4 write code for create() operation ( C R U D )

 

Step 4:

Creating Model for create() method.

Open Member_model.php file that already created in application/models/ directory. We start with createMember() method.  Enter following code:

    public function createMember($data)
    {
        $this->db->trans_start();
        $this->db->insert('user_list', $data);
        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE)
        {
           return false;
        }
        else {
            return true;
        }
    }

user_list is our table from ci-db-tutorial database. This is query for insert data into database. $this->db->trans_status() is help make sure all data is insert into database is success or unsuccess. You can see more example in Codeigniter 3 user guide here.

Creating Controller for create() method.

Open Member.php file that already created in application/controllers/ directory. Go to create() method and enter following code:

( Read code comment for further explanation)

    public function create()
    {
        //use form_validation library
        //filter input for security purpose exp: xss and sql injection
        //warning user if field is empty 
        $this->form_validation->set_rules('member_name', 'Name', 'trim|required');
        $this->form_validation->set_rules('member_gender', 'Gender', 'trim|required');
        $this->form_validation->set_rules('member_address', 'Address', 'trim|required');
       
       //if everyting is clear, start proccess input data
       if ($this->form_validation->run() == TRUE) {
            //all data store in array
            //input->post(); is similar to $GET
            $member_data = array(
                'name'      => $this->input->post('member_name') , 
                'gender'    => $this->input->post('member_gender'),
                'address'   => $this->input->post('member_address')
            );
            //save collected data in array into database using createMember() methods in Member_model
            //$create_user will return boolean TRUE or FALSE condition if data successfully save to database 
            $create_user = $this->Member_model->createMember($member_data);
            //check if data is save successfully in database
            if ($create_user == true) {
                //pop-up message success
                $this->session->set_flashdata('msg_noti', 'Success create user');
                redirect('member/create');
            } else {
                //pop-up message error
                $this->session->set_flashdata('msg_error', 'Error create user');
                redirect('member/create');
            }
            
       } else {
           //pop-up message if field is empty or contain invalid input.
            $this->session->set_flashdata('msg_error', validation_errors());
       }
       //load view
       $this->load->view('member/create');
       
    }

In this method we use:

form_validation library ( Link )

session library ( Link )

database library ( Link )

url helper ( Link )

Member_model Model

Setting validation rules

Codeigniter lets you set as many validation rules as you need for a given field, cascading them in order, and it even lets you prep and pre-process the field data at the same time. To set validation rules you will use the set_rules() method:

$this->form_validation->set_rules();

The above method takes three parameter as input:

  1. The filed name - the exact name you've given the form filed.
  2. A "human" name for the field, which will inserted into the error message. For example, if your field is named "user" you might give it a human name of "Username".
  3. The validation rules for this form field.
  4. (optional) Set custom error messages on any rules given for current field. if not provided will use the default one.

 Visit rules reference: Here

Creating Views for create method:

Done create Model and Controller for create() method, now we need create view or user interface using Bootstrap 3.
Create new member folder inside application/views/ directory. After that, we need create new create.php file inside application/views/member directory.

Open create.php that we already created. Enter following code:

[code]<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Create Member</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Bootstrap 3 -->
    <link rel="stylesheet" type="text/css" media="screen" href="/<?php echo base_url('assets/bootstrap/css/bootstrap.min.css'); ?>" />
    
</head>
<body>
    <!-- alert -->
    
    <?php
        if ($this->session->flashdata('msg_noti') != '') {
            echo
                '<div class="alert alert-success">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                    <p>' . $this->session->flashdata('msg_noti') . '</p>
                </div>';
        }
        if ($this->session->flashdata('msg_error') != '') {
            echo
                '<div class="alert alert-danger">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                    <p>'. $this->session->flashdata('msg_error') . '</p>   
                </div>';
        }
        
    ?>
    
    <!-- /. alert -->
    <div class="container">
        <!-- Form -->
        <form action="<?php echo base_url('Member/create'); ?>" method="POST" class="form-horizontal" role="form">
                <div class="form-group">
                    <legend>Create Member</legend>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-2" for="member_name">Member Name</label>
                    <div class="col-sm-10">
                        <input type="text" name="member_name" id="member_name" class="form-control">                                            
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-2" for="member_gender">Member Gender</label>
                    <div class="col-sm-10">
                        <input type="text" name="member_gender" id="member_gender" class="form-control">                                            
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-2" for="member_address">Member Address</label>
                    <div class="col-sm-10">
                        <input type="text" name="member_address" id="member_address" class="form-control">                                            
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-10 col-sm-offset-2">
                        <button type="submit" class="btn btn-primary">Submit</button>
                        <a class="btn btn-primary" href="/<?php echo base_url('member/index') ?>">Index List</a>
                    </div>
                </div>
        </form>
         <!-- Form -->    
    </div>
    
    <!-- script -->
    <script src="/<?php echo base_url('assets/jquery/jquery-3.3.1.min.js'); ?>"></script>
    <script src="/<?php echo base_url('assets/bootstrap/js/bootstrap.min.js'); ?>"></script>
</body>
</html>[/code]

ci3 create operation

You can get  bootstrap 3 template: Here

For your information

base_url('assets/bootstrap/css/bootstrap.min.css');

will return

http://localhost/mysite/assets/bootstrap/css/bootstrap.min.css

 base_url() returns your site base URL, as specified in your config file. If you have different domain name. Open config.php inside application/config/ directory.

You will see like this:

$config['base_url'] = '';

now change it to this:

$config['base_url'] = 'http://localhost/mysite/';

For now we just create simple input name. gender, and address only. Create operation we done in step 4. If you have problem with create() method, comment below I will try help you. Next step I will show you how to create read() method in Codeigniter 3.

 

Step 5

Creating Model for read() method:

In this step, I will show you how to make read() operation in Codeigniter 3

Open Member_model.php file, enter following code under createMember() method:

    public function getMember($member_id)
    {
        $this->db->select('
            person_id, 
            name, 
            gender,
            address
        ');
        $this->db->from('user_list');
        $this->db->where('person_id', $member_id);
        $query = $this->db->get();
        
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return false;
        }
    }

This function to query single data from database and return data if member_id is exist. If member_id is not exist in database it will return false and controller will trigger error message.
More info about selecting data: Here

Creating Controller for read() method:

Open controller Member.php file from application/controllers/ directory.

Enter following code under create() method.

[code]
public function read($member_id)
    {
        //get single data using member_id
        //using getMember() methods in Member_model to retrieve data from database
        //$data return Array();
       $data = array(
           'userDetail' => $this->Member_model->getMember($member_id) ,
        );
        //load view
        $this->load->view('member/read', $data);
    }
[/code]

In this function we will use userDetail in view or front-end to show data from database.   You can get more info about views in Codeigniter: Here

Creating View for read() method:

Create new read.php file inside application/views/member directory. Open created file,

Enter following code:

[code]
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Read</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Bootstrap 3 -->
    <link rel="stylesheet" type="text/css" media="screen" href="/<?php echo base_url('assets/bootstrap/css/bootstrap.min.css'); ?>" />
    
</head>
<body>
    <div class="container">
    <!-- Read Form -->
        <form role="form" class="form-horizontal">
                <div class="form-group">
                    <legend>Read Member</legend>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-2" for="member_name">Name</label>
                    <div class="col-sm-10">
                        <input type="text" name="member_name" id="member_name" class="form-control" value="<?php echo $userDetail[0]->name; ?>  ">                                          
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-2" for="member_gender">Gender</label>
                    <div class="col-sm-10">
                        <input type="text" name="member_name" id="member_name" class="form-control" value="<?php echo $userDetail[0]->gender; ?>  ">                                             
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-2" for="member_address">Address</label>
                    <div class="col-sm-10">
                        <input type="text" name="member_name" id="member_name" class="form-control" value="<?php echo $userDetail[0]->address; ?>  ">                                             
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-10 col-sm-offset-2">
                        <a class="btn btn-primary" href="/<?php echo base_url('member/index') ?>">Index List</a>
                    </div>
                </div>
        </form>
        <!-- Read Form -->    
    </div>
    <!-- script -->
    <script src="/<?php echo base_url('assets/jquery/jquery-3.3.1.min.js'); ?>"></script>
    <script src="/<?php echo base_url('assets/bootstrap/js/bootstrap.min.js'); ?>"></script>
</body>
</html>
[/code]

ci3 read operation

This view or front-end will show data retrieve name, gender. and address from database. For read() operation we done in step 5. Go next step 6 for update operation in Codeigniter 3.

 

Step 6:

Creating Model for updating() method:

This step will teach you how to make update operation in Codeigniter 3. Open Model Member_model.php file inside application/models/ directory.
In Member_model.php  under getMember() method is updateMember() method.

Enter following code:

[code]
public function updateMember($data, $member_id)
    {
        $this->db->trans_start();
        $this->db->where('person_id', $member_id);
        $this->db->update('user_list', $data);
        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE)
        {
           return false;
        }
        else {
            return true;
        }

    }
[/code]

This method will update data into database. It is accept two parameter $data and $member_id. First parameter is $data from user input and second parameter is $member_id use as reference to update new data. More info updating data in Codeigniter: Here

Creating Controller for update() method:

Open controller Member.php file in application/controllers/ add update function under read function.

Enter following code: 

    public function update($member_id)
    {
        //get data base on Member ID
        $data = array(
           'userDetail' => $this->Member_model->getMember($member_id), 
        );

        //before update new data need filter data first
        //filter input for security purpose exp: xss and sql injection
        //warning user if field is empty
        $this->form_validation->set_rules('member_name', 'Name', 'trim|required');
        $this->form_validation->set_rules('member_gender', 'Gender', 'trim|required');
        $this->form_validation->set_rules('member_address', 'Address', 'trim|required');

        //if everiting is clear, start proccess input data
        if ($this->form_validation->run() == true) {
            //store date inside array 
            $new_data = array(
                'name'      => $this->input->post('member_name'),
                'gender'    => $this->input->post('member_gender'),
                'address'   => $this->input->post('member_address') 
            );
            //save collected data from $new_data array into database using updateMember() methods in Member_model
            //$create_user will return boolean TRUE or FALSE condition if data successfully save to database 
            $update_data = $this->Member_model->updateMember($new_data, $member_id);
             //check if data is save successfully in database
            if ($update_data == true) {
                //pop-up message success
                $this->session->set_flashdata('msg_noti', 'Success update Member');
                redirect('member/update/'. $member_id);
            } else {
                //pop-up message error
                $this->session->set_flashdata('msg_error', 'Error update Member');
                redirect('member/update/'. $member_id);
            }
        } else {
            //pop-up message validatin error
            $this->session->set_flashdata('msg_error', validation_errors());
        }
        //load view
        $this->load->view('member/update', $data);
    }

This function combine with create and read function. All data retrieve base on $member_id then update data to database.

Creating View for update function:

Next, create new view update.php file inside application/views/member/ directory. Open file that you already created.

Enter following code:

 <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Update Member</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" media="screen" href="/<?php echo base_url('assets/bootstrap/css/bootstrap.min.css'); ?>" />
    
</head>
<body>
    <!-- alert -->
    
    <?php
        if ($this->session->flashdata('msg_noti') != '') {
            echo 
                '<div class="alert alert-success">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                    <p>' . $this->session->flashdata('msg_noti') . '</p>
                </div>';
        } 
        if ($this->session->flashdata('msg_error') != '') {
            echo 
                '<div class="alert alert-danger">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                    <p>'. $this->session->flashdata('msg_error') . '</p>   
                </div>';
        }
        
    ?>
    
    <!-- /. alert -->
    <div class="container">
        <!-- Update Form -->
        <form action="<?php echo base_url('Member/update/'. $userDetail[0]->person_id ); ?>" method="POST" class="form-horizontal" role="form">
                <div class="form-group">
                    <legend>Update Member</legend>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-2" for="member_name">Name</label>
                    <div class="col-sm-10">
                        <input type="text" name="member_name" id="member_name" class="form-control" value="<?php echo $userDetail[0]->name; ?>">                                            
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-2" for="member_gender">Gender</label>
                    <div class="col-sm-10">
                        <input type="text" name="member_gender" id="member_gender" class="form-control" value="<?php echo $userDetail[0]->gender; ?>">                                            
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-2" for="member_address">Address</label>
                    <div class="col-sm-10">
                        <input type="text" name="member_address" id="member_address" class="form-control" value="<?php echo $userDetail[0]->address; ?>">                                            
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-10 col-sm-offset-2">
                        <button type="submit" class="btn btn-primary">Submit</button>
                        <a class="btn btn-primary" href="/<?php echo base_url('member/index') ?>">Index List</a>
                    </div>
                </div>
        </form>
        <!-- Update Form -->    
    </div>
    
    <!-- script -->
    <script src="/<?php echo base_url('assets/jquery/jquery-3.3.1.min.js'); ?>"></script>
    <script src="/<?php echo base_url('assets/bootstrap/js/bootstrap.min.js'); ?>"></script>
</body>
</html>

ci3 update operation

This view will get member info from controller based on person_id in database. After finished edit/update member info, it will submit back to controller to check xss or sql injection before send it to model update() method.

For update operation in codeigniter is done in step 6. Next step you will learn how to delete data in database.

 

Step 7

This step you will learn how to delete data base on person_id in database. Delete operation no need create view, just need create model and controller.

Creating Model for delete method:

Open model Member_model.php file in application/models/ directory. Enter following code under updateMember() method:

    public function deleteMember($member_id)
    {
        $this->db->trans_start();
        $this->db->where('person_id', $member_id);
        $this->db->delete('user_list');
        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE)
        {
           return false;
        }
        else {
            return true;
        }
    }

More detail about delete query in Codeigniter: Here

Creating Controller for delete() method:

Open model Member.php file inside application/controllers/ directory. Under update() method is delete() method, enter following code:

    public function delete($member_id = false)
    {
        //Make sure member_id is exist 
        if ($member_id != false) {
            //if member_id is exist then start delete member from database using deleteMember() methods in Member_model
            $delete_member = $this->Member_model->deleteMember($member_id);
            //Make sure Member is successfuly remove from database
            if ($delete_member == true) {
                //pop-up message success
                $this->session->set_flashdata('msg_noti', 'Success remove member');
                redirect('member/index');   
            }
        } else {
            //pop-up message error if member is not exsit.
            $this->session->set_flashdata('msg_error', 'Member not exist');
            redirect('member/index');
        }
        
    }

This method will make sure $member_id is exist or not to protect other data from deleted.

For delete operation in Codeigniter is done in step 7. Next step you will learn how to index data from database using DataTable.js plugin.

 

Step 8

Creating Model for index method:

This step you will learn how to get all data from database using DataTable.js plugin. Open model Member_model.php file inside application/models/ directory.

Enter following code under deleteMember() method:

    public function getindex()
    {
        $this->db->from('user_list');
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return false;
        }
    }

This method will get whole selected table data in database. For more info about select query: Here

Creating controller for index method:

Open controller Member.php file inside application/controllers/ directory.

Enter following code, under delete() method:

    public function index()
    {
      //get data from Member_modal using getindex() methods
      $data = array(
          'userList' => $this->Member_model->getindex(), 
      );
      //load view
      $this->load->view('member/index', $data);
    }

Creating View for index method:

Next, we create view file for index. In this view we need declare DataTables.js at HTML header and footer. Create new view index.php file inside application/views/member/ directory.

Enter following code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Index</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Bootstrap 3 -->
    <link rel="stylesheet" type="text/css" media="screen" href="/<?php echo base_url('assets/bootstrap/css/bootstrap.min.css'); ?>" />
    <!-- DataTables -->
    <link rel="stylesheet" type="text/css" media="screen" href="/<?php echo base_url('assets/DataTables/datatables.min.css'); ?>" />
    
</head>
<body>
    <nav class="navbar navbar-default">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" href="#">jaironlanda.com</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
        <ul class="nav navbar-nav">
            <li class="active"><a href="/<?php echo base_url('Member/index'); ?>">Index <span class="sr-only">(current)</span></a></li>
            <li><a href="/<?php echo base_url('Member/create'); ?>">Create</a></li>
        </ul>
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
    </nav>
        <!-- alert -->
        <?php
            if ($this->session->flashdata('msg_noti') != '') {
                echo 
                    '<div class="alert alert-success">
                        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                        <p>' . $this->session->flashdata('msg_noti') . '</p>
                    </div>';
            } 
            if ($this->session->flashdata('msg_error') != '') {
                echo 
                    '<div class="alert alert-danger">
                        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                        <p>'. $this->session->flashdata('msg_error') . '</p>   
                    </div>';
            }
            
        ?>
        <!-- /. alert -->
        <div class="container">
        <a class="btn btn-success" href="/<?php echo base_url('Member/create') ?>"> 
            <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
            Create Member
        </a>
        <!-- DataTable -->
        <table id="user-table" class="table table-striped table-hover">
            <thead>
                <tr>
                    <th>No.</th>
                    <th>Name</th>
                    <th>Gender</th>
                    <th>Address</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <?php 
                    $no = 1;
                    if ($userList != false) {
                        foreach ($userList as $userData) {
                            echo '<tr>';
                            echo '<td>'.$no++.'</td>';
                            echo '<td>'.$userData->name.'</td>';
                            echo '<td>'.$userData->gender.'</td>';
                            echo '<td>'.$userData->address.'</td>';
                            echo '
                                <td>
                                    <a class="btn btn-primary" href="'.base_url('Member/read/'.$userData->person_id).'"><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span> Read</a> 
                                    <a class="btn btn-default" href="'.base_url('Member/update/'.$userData->person_id).'"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> Edit</a> 
                                    <a class="btn btn-danger" href="'.base_url('Member/delete/'.$userData->person_id).'"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</a>
                                </td>';
                            echo '</tr>';
                        }
                    }
                ?>
            </tbody>
        </table>
    <!-- /.DataTable -->
    </div>
    <!-- script -->
    <script src="/<?php echo base_url('assets/jquery/jquery-3.3.1.min.js'); ?>"></script>
    <script src="/<?php echo base_url('assets/bootstrap/js/bootstrap.min.js'); ?>"></script>
    <script src="/<?php echo base_url('assets/DataTables/datatables.min.js'); ?>"></script>
    <script>
       $(document).ready(function() {
            $('#user-table').DataTable();
        });
    </script>
</body>
</html>

Note: Don't forget add DataTables script after add datables.min.js.

Check here for more info about DataTables: Here

ci3 index operation 

Codeigniter C.R.U.D operation tutorial is end here. If you have problem regarding this topic you can write your comment bellow. Happy coding :D 

 

 

Demo link: Here

Github link: Here

Source code : Download