CodeIgniter Insert, Update, Delete, Form validation, Pagination

CodeIgniter Insert, Update, Delete, Form validation, Pagination

Step 1) Create Table :

First, we need one table for the store out data. Below table query is given use that.

CREATE TABLE IF NOT EXISTS `shades` (
`shade_id` int(11) NOT NULL AUTO_INCREMENT,
`shade_name` varchar(100) DEFAULT NULL,
PRIMARY KEY (`shade_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1014 DEFAULT CHARSET=latin1;




 

Step 2)  Make view :

Make html form design using boostrap. Code Given below just copy and save file in view folder and give name add_shade_view.php

<div id=”page-wrapper”>
<div class=”row”>
<div class=”col-lg-12″>
<h1 class=”page-header”>Shade</h1>
</div>
<!– /.col-lg-12 –>
</div>
<!– /.row –>
<div class=”row”>
<div class=”col-lg-12″>
<div class=”panel panel-default”>
<div class=”panel-heading”>
Add new shade
</div>
<div class=”panel-body”>
<div class=”row”>
<div class=”col-lg-6″>
<?php if($error=$this->session->flashdata(‘error’)):
$errorClass=$this->session->flashdata(‘error-class’);
?>
<div class=”alert <?=$errorClass?> alert-dismissable”>
<button type=”button” class=”close” data-dismiss=”alert” aria-hidden=”true”>×</button>
<?php print_r($error); ?>
</div>
<?php endif; ?>
<?php echo form_open(‘shade/store_shade’); ?>
<div class=”form-group”>
<label>Shade</label>
<?php echo form_input([‘name’=>’shade_name’,’class’=>’form-control’,’placeholder’=>’Enter shade name’,’autofocus’=>’autofocus’,’value’=>set_value(‘shade_name’)]);?>
<?php
echo form_error(‘shade_name’) ;
?>
</div>
<?php echo form_submit([‘name’=>’save’,’value’=>’Save’,’class’=>’btn btn-primary’]); ?>
<a href=”<?= base_url(‘shade/shade_list’);?>” class=”btn btn-default”>Back</a>
<?= form_close();?>
</div>
</div>
<!– /.row (nested) –>
</div>
<!– /.panel-body –>
</div>
<!– /.panel –>
</div>
<!– /.col-lg-12 –>
</div>
<!– /.row –>

</div>
<!– /#page-wrapper –>


Step 3)  Make Controller :

Make Controller file file for shade. Code Given below just copy and save file in controller folder and give name Shade.php

<?php
class Shade extends CI_Controller{

function __construct()
{ // check admin is login or not ?
parent::__construct();
if(!$this->session->userdata(‘admin_id’))
return redirect(‘admin_in/’);
}

public function index()
{ // redirect to login screen
if($this->session->userdata(‘admin_id’))
//return redirect(‘admin/dashboard’);
return redirect(‘admin/Employee’);

$this->load->helper(‘form’); // autoload
$this->load->view(‘admin/login’);
}

public function shade_list(){

$this->load->model(‘Shade_model’);

$this->load->library(‘pagination’);

$config = [
‘base_url’ => base_url(‘shade/shade_list’),
‘per_page’ => 20,
‘total_rows’ => $this->Shade_model->num_rows(),
‘num_links’ => 9,
‘full_tag_open’ => “<ul class=’pagination’>”,
‘full_tag_close’ => “</ul>”,
‘first_tag_open’ => ‘<li>’,
‘first_tag_close’ => ‘</li>’,
‘last_tag_open’ => ‘<li>’,
‘last_tag_close’ => ‘</li>’,
‘next_tag_open’ => ‘<li>’,
‘next_tag_close’ => ‘</li>’,
‘prev_tag_open’ => ‘<li>’,
‘prev_tag_close’ => ‘</li>’,
‘num_tag_open’ => ‘<li>’,
‘num_tag_close’ => ‘</li>’,
‘cur_tag_open’ => “<li class=’active’><a>”,
‘cur_tag_close’ => ‘</a></li>’,
];

$this->pagination->initialize($config);

$data=$this->Shade_model->shade_data($config[‘per_page’],$this->uri->segment(3));
$this->load->view(‘admin/shade_view’,[‘types’=>$data]);
}

public function add_shade()
{
$this->load->helper(‘form’); // autoload
$this->load->view(‘admin/add_shade_view’);
}

public function store_shade(){

$this->load->library(‘form_validation’);
$this->form_validation->set_error_delimiters(‘<p class=”text-danger”>’,'</p>’);
if($this->form_validation->run(‘shade’)){ // if validation true
$data=$this->input->post();
$this->load->model(‘Shade_model’);
if($this->Shade_model->add_shade($data)){
$this->session->set_flashdata(‘error’,’Shade save successfully’);
$this->session->set_flashdata(‘error-class’,’alert-success’);
}else{
$this->session->set_flashdata(‘error’,’Shade dose not save, Please try again!!!’);
$this->session->set_flashdata(‘error-class’,’alert-danger’);
}
return redirect(‘shade/shade_list’);
}else{
$this->add_shade();
}
}

public function edit_shade_view($shade_id){

$this->load->model(‘Shade_model’,’shade’);
$shade = $this->shade->find_shade($shade_id);
//print_r($shade);
$this->load->view(‘admin/edit_shade_view’,[‘shade’=>$shade]);
}

public function update_shade($shade_id)
{
$this->form_validation->set_error_delimiters(‘<p class=”text-danger”>’,'</p>’);
if($this->form_validation->run(‘shade’)){ // if validation true
$data=$this->input->post();
$this->load->model(‘Shade_model’,’shade’);
if($this->shade->update_shade($shade_id,$data)){
$this->session->set_flashdata(‘error’,’Shade update successfully’);
$this->session->set_flashdata(‘error-class’,’alert-success’);
}else{
$this->session->set_flashdata(‘error’,’Shade dose not update, Please try again!!!’);
$this->session->set_flashdata(‘error-class’,’alert-danger’);
}
return redirect(‘shade/shade_list’);
}else{
$this->edit_shade_view($shade_id);
}
}

public function delete_shade($shade_id)
{
$this->load->model(‘Shade_model’,’shade’);
if($this->shade->delete_shade($shade_id)){
$this->session->set_flashdata(‘error’,’Shade delete successfully’);
$this->session->set_flashdata(‘error-class’,’alert-success’);
}else{
$this->session->set_flashdata(‘error’,’Shade dose not delete, Please try again!!!’);
$this->session->set_flashdata(‘error-class’,’alert-danger’);
}
return redirect(‘shade/shade_list’);
}
}


Step 4)  Make Model :

Make Model file for shade. Code Given below just copy and save file in model folder and give name Shade_model.php

<?php
class Shade_model extends CI_Model{

public function all_shade_data(){

$data=$this->db
->select([‘shade_id’,’shade_name’])
->from(‘shades’)
->order_by(‘shade_name’,’ASC’)
->get();
return $data->result_array();
}
public function shade_data($limit,$offset){

$data=$this->db
->select([‘shade_id’,’shade_name’])
->from(‘shades’)
->limit($limit,$offset)
->order_by(‘shade_name’,’ASC’)
->get();
return $data->result();
}

public function num_rows(){

$data=$this->db
->select([‘shade_id’,’shade_name’])
->from(‘shades’)
->get();
return $data->num_rows();
}

public function add_shade($data){
return $this->db->insert(‘shades’,[‘shade_name’=>$data[‘shade_name’]]);
}

public function find_shade($shade_id)
{
//exit($ac_type_id);
$q = $this->db->select([‘shade_id’,’shade_name’])
->where(‘shade_id’, $shade_id)
->order_by(‘shade_name’,’ASC’)
->get(‘shades’);

return $q->row();
}

public function update_shade($shade_id,$data)
{
return $this->db
->where(‘shade_id’,$shade_id)
->update(‘shades’,[‘shade_name’=>$data[“shade_name”]]);

}

public function delete_shade($shade_id)
{
return $this->db
->where(‘shade_id’,$shade_id)
->delete(‘shades’);

}
}
?>


Step 5)  Show inserted data View in the table :

Make View file for shade. Code Given below just copy and save file in view folder and give name shade_view.php

<div id=”page-wrapper”>
<div class=”row”>
<div class=”col-lg-12″>
<h1 class=”page-header”>Shade</h1>
</div>
<!– /.col-lg-12 –>
</div>
<!– /.row –>
<div class=”row”>
<div class=”col-lg-12″>
<?php if($error=$this->session->flashdata(‘error’)):
$errorClass=$this->session->flashdata(‘error-class’);
?>
<div class=”alert <?=$errorClass?> alert-dismissable”>
<button type=”button” class=”close” data-dismiss=”alert” aria-hidden=”true”>×</button>
<?php print_r($error); ?>
</div>
<?php endif; ?>
</div>
<div class=”col-lg-12″>
<div class=”panel panel-default”>
<div class=”panel-heading”>
<a href=”<?= base_url(‘shade/add_shade’);?>” class=”btn btn-outline btn-primary ” >Add New</a>
</div>
<!– /.panel-heading –>
<div class=”panel-body”>
<table width=”100%” class=”table table-striped table-bordered table-hover” id=”dataTables-example”>
<thead>
<tr>
<th>No</th>
<th>Shade</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
if(count($types)):
$count=$this->uri->segment(3);
foreach($types as $data):
?>
<tr class=”odd gradeX”>
<td><?= ++$count; ?></td>
<td><?= $data->shade_name ?></td>
<td class=”center”>
<a href=”<?= base_url(‘shade/edit_shade_view/’.$data->shade_id);?>” class=”btn btn-outline btn-info”>Edit</a>
<a href=”<?= base_url(‘shade/delete_shade/’.$data->shade_id);?>” class=”btn btn-outline btn-danger”>Delete</a>
</td>
</tr>
<?php
endforeach;
else:
?>
<tr class=”odd gradeX”>
<td colspan=”3″ style=”text-align:center”><b>No records found!!!</b></td>
</tr>
<?php
endif;
?>
</table>
<?= $this->pagination->create_links(); ?>
</div>
<!– /.panel-body –>
</div>
<!– /.panel –>
</div>
<!– /.col-lg-12 –>
</div>
<!– /.row –>

</div>
<!– /#page-wrapper –>


Step 6)  Edit inserted Data :

Make View file for edit shade. Code Given below just copy and save file in view folder and give name edit_shade_view.php

<div id=”page-wrapper”>
<div class=”row”>
<div class=”col-lg-12″>
<h1 class=”page-header”>Shade</h1>
</div>
<!– /.col-lg-12 –>
</div>
<!– /.row –>
<div class=”row”>
<div class=”col-lg-12″>
<div class=”panel panel-default”>
<div class=”panel-heading”>
Edit shade
</div>
<div class=”panel-body”>
<div class=”row”>
<div class=”col-lg-6″>
<?php if($error=$this->session->flashdata(‘error’)):
$errorClass=$this->session->flashdata(‘error-class’);
?>
<div class=”alert <?=$errorClass?> alert-dismissable”>
<button type=”button” class=”close” data-dismiss=”alert” aria-hidden=”true”>×</button>
<?php print_r($error); ?>
</div>
<?php endif; ?>
<?php echo form_open(“shade/update_shade/{$shade->shade_id}”); ?>
<div class=”form-group”>
<label>Shade Name</label>
<?php echo form_input([‘name’=>’shade_name’,’class’=>’form-control’,’placeholder’=>’Enter shade name’,’autofocus’=>’autofocus’,’value’=>set_value(‘cformname’,$shade->shade_name)]);?>
<?php
echo form_error(‘shade_name’) ;
?>
</div>
<?php echo form_submit([‘name’=>’save’,’value’=>’Save’,’class’=>’btn btn-primary’]); ?>
<a href=”<?= base_url(‘shade/shade_list’);?>” class=”btn btn-default”>Back</a>
<?= form_close();?>
</div>
</div>
<!– /.row (nested) –>
</div>
<!– /.panel-body –>
</div>
<!– /.panel –>
</div>
<!– /.col-lg-12 –>
</div>
<!– /.row –>

</div>
<!– /#page-wrapper –>