Date :28/03/2019

What is codeigniter?

Codeigniter is a php framework to develop the application. Therefore, it provides the many libraries for connecting to the database.

Codeigniter simple Add, Edit, Delete, View

However, we create database and table.

Firstly, database name is considered to be as ‘form’.

similarly, creating a table named ‘register’.

Database Config Settings

In order to connect with the database, the below instructions must be followed.

The file is located at application/config/database.php.

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

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


Controller (application/controllers/Welcome.php)

Controller is a process which get the input request from user, communicates with Model and then loads appropriate Views file.

In the below Controller class, we have functions for different tasks.

public function list ()
  {
    $this->load->database();
    $this->load->model('Insert_model');
    $data['table_items']=$this->Insert_model->list();
    $this->load->view('list_client', $data);
  }
	
  public function create (){
		$this->load->view('display.php');
	}
	public function add()
	{
		$this->load->model('Insert_model');
		$postData = array(
		'id' => $this->input->post('id'),
    'first_name' => $this->input->post('firstname'),
    'gender' => $this->input->post('gender'),
    'habbies' => $this->input->post('hobbies'),
    'state' => $this->input->post('state'),
    'descrite' => $this->input->post('discription'));    
    $insert = $this->Insert_model->insert($postData);
  }
  
  public function retrive_update_data($id)
  {
    $this->load->model('Insert_model');
    $da= $this->Insert_model->retrive_update_data($id);
    $data['value1'] = $da->row();       
    $this->load->view('update_view', $data);
  }
 
  public function update_data($id)
  {
    $this->load->model('Insert_model');
    $postData = array(
		'first_name' => $this->input->post('firstname'),
    'gender' => $this->input->post('gender'),
    'habbies' => $this->input->post('habbies'),
    'state' => $this->input->post('state'),
    'descrite' => $this->input->post('describe'));    
     $da= $this->Insert_model->update_data_model($id, $postData);
  }

   public function delete($id)
  {
    $this->load->model('Insert_model');
    $delete = $this->Insert_model

Model (application/models/Insert_model.php)

This class is responsible for interacting with database for data select, insert, update and delete purposes.

-> To list() function fetches/selects register items by $slug name.
->In addition to insert() function insert new item to register
-> In order to update_data_model() function either update register item.
-> Most importantly, retrive_update_data() function fetches register item by it’s ID
->Finally, delete() function delets any particular register item.

  public function list($data = array())
  {
    $insert = $this->db->get('register')->result();
    return $insert;
  }

  public function insert($data = array())
	{
    $insert=$this->db->insert('register',$data);
  }
	
  public function retrive_update_data($id)
  {
   		$this->db->select('*');
       return $query = $this->db->get_where('register',array('id'=>$id));
  }

  public function update_data_model($id, $postData)
  {
      $this->db->where('id', $id);
      $this->db->update('register', $postData);
      echo "data updated successfully";
  }

  public function delete($id)
  {
    $this->db->where('id', $id);
    $this->db->delete('register');
    echo "value deleted successfully";
  }

}


Index View (application/views/list_client.php)

This view file is called by list() function of our Controller class because it will lists out all the register items.

<body>
  <center>
<table border="1">
<thead>
      <tr>
          <th>ID</th>
          <th>first-name</th>
          <th>gender</th>
          <th>hobbies</th>
          <th>state</th>
          <th>discription</th>
          <th>Delete</th>
      </tr>
    </thead>
    <tbody>
       <?php foreach($table_items as $row){ ?>
         <tr>
            <td><?php echo $row->id; ?></td>
                <td><?php echo $row->first_name; ?></td>
                <td><?php echo $row->gender; ?></td>
                <td><?php echo $row->habbies; ?></td>
                <td><?php echo $row->state; ?></td>
                <td><?php echo $row->descrite; ?></td>
<td><a href="<?php echo site_url('Welcome/delete/'.$row->id);?>">Delete</a></td>
<td><a href="<?php echo site_url('Welcome/retrive_update_data/'.$row->id);?>">Edit</a></td></tr>
  <?php } ?>
</tbody>

Add View(application/views/add_new.php)

This view file is called by create() function of our Controller class and then most certainly, it will show the new form to add item in register.

<body>

<form role="form" method="post" action="<?php echo site_url('Welcome/add');?>">
 First name:<br> <input type="text" name="firstname">
 <br>
 gender<input type="radio" name="gender" value="male" checked> Male<br>
 <input type="radio" name="gender" value="female"> Female<br>
 <input type="radio" name="gender" value="other"> Other
 <br>
 Hobbies<br><input type="checkbox" name="hobbies" value="dancing">dancing<br>
<input type="checkbox" name="hobbies" value="dancing">playing<br>
 <br>
State<br> <input type="text" name="state" value="">
 <br>
  Discription<br><input type="text area" name="discription">
</textarea>
 <br>

 <input type="submit">
</form>

</body>

Update View
(application/views/update_view.php)

This view file is called by retrive_update_data() function of our Controller class. It show the form with user data.

<body>
	<form role="form" method="post" action="<?php echo site_url('Welcome/update_data/' .$value1->id);?>">

<label> First name:</label><br> <input type="text" name="firstname" value="<?php echo $value1->first_name;?>;">  <br>
 <label> Gender:</label><br> <input type="text" name="gender" value="<?php echo $value1->gender;?>;">  <br>


<label> Hobbies:</label><br> <input type="text" name="hobies" value="<?php echo $value1->habbies;?>;">  <br>

state<br> <input type="text" name="state" value="<?php echo $value1->state;?>;">  <br>
  Discription<br><input type="text area" name="discription" value="<?php echo $value1->descrite;?>;">  <br>
<button> Submit</button>
</form>

Thanks for using pheonix solutions.

You find this tutorial helpful? Share with your friends to keep it alive.

1 thought on “CODEIGNITER: SIMPLE ADD, EDIT, DELETE, VIEW”

Leave a Reply