Learn Codeigniter

CodeIgniter CURD

We will perform CURD(Create Read Update Delete) operation using CodeIgniter framework. We will use following step to perform CURD operation.

  1. First we create database and tables. CREATE DATABASE crud;
    CREATE TABLE employee(
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(50) DEFAULT NULL,
      `gender` char(20) DEFAULT NULL,
       PRIMARY KEY (`id`)
    );
    INSERT INTO `employee`(`id`,`name`,`gender`) VALUES (NULL,'Manish','Male');
    INSERT INTO `employee`(`id`,`name`,`gender`) VALUES (NULL,'Pawan','Male');
    INSERT INTO `employee`(`id`,`name`,`gender`) VALUES (NULL,'Kripal','Male');
    INSERT INTO `employee`(`id`,`name`,`gender`) VALUES (NULL,'Pradeep','Male'); 
    
  2. Open the application/config/config.php file and set your base URL to your localhost path.

    $config['base_url'] = "http://localhost/CodeIgniter_2.2.0/";
  3. open the application/config/database.php and set your database settings.

    $db['default']['hostname'] = 'localhost';
    $db['default']['username'] = 'root';
    $db['default']['password'] = '*****'; 
    $db['default']['database'] = 'curd';
  4. Open the application/config/autoload.php file and add url helper.

    $autoload['helper'] = array('url');
  5. Open the application/config/routes.php file and set your Default controller.

  6. $route['default_controller'] = "employee/add";
  7. Create view files inside application\views\employeeadd.php to add Employee.

    <html>
    <head>
    <title>Curd in Codeigniter</title>
    </head>
    <body>
    <h3>Curd in Codeigniter</h3>
    <a href="<?php echo  base_url();?>employee/add">Add Employee</a>
    <a href="<?php echo  base_url();?>employee/allemployee">Employee List</a>
    <br>
    <?php if($this->session->flashdata('msg')) { 
    echo $this->session->flashdata('msg');
    } ?>
    <br>
    <form name="add"   method="POST" action="<?php echo  base_url();?>employee/add">
    <table border="1">
    <tr>
    <th>Name</th>
    <td><input type="text" name="name"/></td>
    </tr>
    <tr>
    <th>Gender </th>
    <td><input type="text" name="gender"/></td>
    </tr>
    <tr>
    <td><input type="submit" name="submit" value="Add"></td>
    <td><input type="reset" value="Reset" ></td>
    </tr>
    </table> 
    </form>
    </body>
    </html>

    Create Second view file inside application\views\employeelist.php to list all Employee.

    <html>
    <head>
    <title>Curd in Codeigniter</title>
    </head>
    <body>
    <h3>Employee List</h3>
    <a href="<?php echo  base_url();?>employee/add">Add Employee</a>
    <a href="<?php echo  base_url();?>employee/allemployee">Employee List</a>
    <br>
    <br>
    <table border="1">
    <tr>
    <th>Name</th>
    <th>Gender</th>
    <th>Edit </th>
    <th>Delete </th>
    </tr>
    <?php 
         for($i=0; $i<count($employeelist);$i++) {
    ?>
    <tr>
    <td><?php echo $employeelist[$i]->name;?></td>
    <td><?php echo $employeelist[$i]->gender;?></td>
    <td>
    <a href="<?php echo base_url(); ?>employee/edit/<?php echo $employeelist[$i]->id;?>">edit
    </a></td>
    <td><a href="<?php echo base_url(); ?>employee/delete/<?php echo $employeelist[$i]->id;?>" onclick="return confirm('are you sure to delete employee')">delete</a>
    </td>
    </tr>
    <?php }?>  
    </table> 
    </body>
    </html>

    Create third view file inside application\views\employeeupdate.php to update Employee.

    <html>
    <head>
    <title>Curd in Codeigniter</title>
    </head>
    <body>
    <h3>Employee Update</h3>
    <a href="<?php echo  base_url();?>employee/add">Add Employee</a>
    <a href="<?php echo  base_url();?>employee/allemployee">Employee List</a>
    <br>
    <?php if($this->session->flashdata('msg')) { 
    echo $this->session->flashdata('msg');
    } ?>
    <br>
    <form name="add" method="POST" action="<?php echo base_url() ?>employee/edit/
    <?php echo $employee[0]->id;?>">     
    <table border="1">
    <tr>
    <th>Name</th>
    <td>
    <input type="text" name="name" value="<?php echo $employee[0]->name;?>" />
    </td>
    </tr>
    <tr>
    <th>Gender </th>
    <td><input type="text" name="gender" value="<?php echo $employee[0]->gender;?>" /></td>
    </tr>
    <tr  >
    <td colspan="2"><input type="submit" name="submit" value="Update"></td>
    </tr>
    </table> 
    </form>
    </body>
    </html>
  8. Create Controller file inside application/controllers/employee.php .

    <?php
    class employee extends CI_Controller{
     function __construct()
    {
    parent::__construct();
    $this->load->model('employeemodel');
    $this->load->helper('url');
    }
          public function add()
        {
        
    if($this->input->post('submit'))
          {
    $employee=array('name' =>$this->input->post('name'),'gender' =>$this->input->post('gender'));
    $this->employeemodel->add_employee($employee);
    $this->session->set_flashdata('msg',"Employee has been addedd successfully");
    $base_url=base_url();
    redirect("$base_url"."employee/add/");
           }
    $this->load->view('employeeadd');
      }
    function allemployee()
    {
    $data['employeelist']=$this->employeemodel->get_entries();
    $this->load->view('employeelist',$data);
    }
        public function edit($id)
      {
         if($this->input->post('submit'))
    {
    $employee=array('name' =>$this->input->post('name'),'gender' =>$this->input->post('gender'));
    $this->employeemodel->update_entry($employee,$id);
    $this->session->set_flashdata('msg',"Employee has been updated successfully");
    $base_url=base_url();
    redirect("$base_url"."employee/edit/$id");
       }
      $data['employee']=$this->employeemodel->get($id);
    $this->load->view('employeeupdate',$data);
        }
    public function delete($id)
        {
    
           $this->employeemodel->delete_entry($id);
            
      $data['employeelist']=$this->employeemodel->get_entries();
       $this->load->view('employeelist',$data);
                          
    }
       }
    ?>
  9. Create Model files inside application/models/employeemodel.php .

    <?php
    class employeemodel extends CI_Model {
     function __construct()
        {
                  parent::__construct();
        }
        function add_employee($employee)
    {
        $this->db->insert('employee',$employee);
    
    }   
    function get_entries()
    {
     $query = $this->db->get('employee');
      return $query->result();
        }
    function update_entry($employee,$id)
        {
             
     $this->db->where('id',$id);
       $this->db->update('employee',$employee);
            }
    
      function get($id){
        $sql = "SELECT * FROM employee WHERE id = ?";
        $query =$this->db->query($sql, array($id)); 
                return $query->result();
        }
     function delete_entry($id)
        {
           $this->db->delete('employee', array('id' => $id));
        }
           }
    ?>