Codeigniter
Crud Example In Codeigniter
In this post we will learn , CRUD example in codeigniter.
Step 1 : Create Table:
CREATE TABLE IF NOT EXISTS `emp` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `mobno` bigint(10) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Step 2 :Application/config/database.php
defined('BASEPATH') OR exit('No direct script access allowed'); $active_group = 'default'; $query_builder = TRUE; $db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => 'root', 'database' => 'test', '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 );
Step 3 : Create Controller (CRUD.php):
defined('BASEPATH') OR exit('No direct script access allowed'); class Crud extends CI_Controller { public function __construct(){ parent::__construct(); $this->load->library('form_validation'); $this->load->library('session'); $this->load->model('CRUDModel'); } public function index(){ $result['datas']=$this->CRUDModel->showAll(); $this->load->view('list',$result); } public function create(){ $this->load->view('create'); } public function store(){ $this->form_validation->set_rules('name', 'Name ', 'required'); $this->form_validation->set_rules('email', 'Email ', 'required'); $this->form_validation->set_rules('mobno', 'Mobile No ', 'required'); if ($this->form_validation->run() == FALSE){ $this->load->view('create'); }else{ $retval = $this->CRUDModel->save($this->input->post()); $this->session->set_flashdata('message','Data Saved Successfully'); $this->index(); } } public function edit(){ $result['data'] = $this->CRUDModel->showById($this->input->post('id')); $this->load->view('edit',$result); } public function update(){ $this->form_validation->set_rules('name', 'Name ', 'required'); $this->form_validation->set_rules('email', 'Email ', 'required'); $this->form_validation->set_rules('mobno', 'Mobile No ', 'required'); $result['data'] = $this->CRUDModel->showById($this->input->post('id')); if ($this->form_validation->run() == FALSE){ $this->load->view('edit',$result); }else{ $retval = $this->CRUDModel->update($this->input->post()); $this->session->set_flashdata('message','Data Updated Successfully'); $this->index(); } } public function delete(){ $this->CRUDModel->delete($this->input->post('id')); $this->session->set_flashdata('message','Data Deleted Successfully'); $this->index(); } }
Step 4 : Create Model(CRUDModel.php):
defined('BASEPATH') OR exit('No direct script access allowed'); class CrudModel extends CI_Model{ private $table = 'emp'; public function save($data){ unset($data['submit']); if($this->db->insert($this->table,$data)){ return true; }else{ return false; } } public function showAll(){ $datas = $this->db->get($this->table); return $datas->result(); } public function showById($id){ $data = $this->db->get_where($this->table,array('id'=>$id)); return $data->row(); } public function update($data){ $array = array('name'=>$data['name'],'email'=>$data['email'],'mobno'=>$data['mobno']); $this->db->where('id', $data['id']); $this->db->update($this->table, $array); return true; } public function delete($id){ $this->db->where('id', $id); $this->db->delete($this->table); return true; } }
Step 5 : Create View(list.php):
<html lang="en"> <head> <title>Ci Crud Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script> </head> <body> <h1 class="text-success" align="center">CI Crud Example</h1><br> <div class="container"> <a href="<?php echo site_url();?>/crud/create" class="btn btn-primary pull-right">Insert</a> <br><br> <h4 class="text text-success"><?php echo $this->session->flashdata('message'); ?></h4> <table class="table table-striped table-hover table-bordered"> <thead> <tr class="active"> <th>Sl No.</th> <th>Name</th> <th>Email</th> <th>Mobile No</th> <th>Edit</th> <th>Delete</th> </tr> </thead> <tbody> <tr> <?php $sn=1;?> <?php foreach($datas as $data) { ?> <td><?php echo $sn++;?> <td><?php echo $data->name;?></td> <td><?php echo $data->email;?></td> <td><?php echo $data->mobno;?></td> <form method="post" action="<?php echo site_url();?>/crud/edit"> <td><button type="submit" class="btn btn-primary"> <span class="glyphicon glyphicon-pencil"></span> <input type="hidden" value="<?php echo $data->id;?>" name="id"> </button> </td> </form> <form method="post" action="<?php echo site_url();?>/crud/delete"> <td><button type="submit" class="btn btn-danger"> <span class="glyphicon glyphicon-remove"></span> <input type="hidden" value="<?php echo $data->id;?>" name="id"> </button> </td> </form> </tr> <?php } ?> </tbody> </table> </div> </body> </html>
Step 5 : Create View(create.php):
<!DOCTYPE html> <html lang="en"> <head> <title>Ci Crud Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script> </head> <body> <h1 class="text-success" align="center">CI Crud Example</h1><br> <a href="<?php echo site_url();?>/crud" class="btn btn-primary pull-right">Back</a> <br><br> <h4 class="text-danger"><?php echo validation_errors(); ?></h4> <div class="container"> <form class="form-horizontal" method="post" action="<?php echo site_url();?>/crud/store"> <div class="form-group"> <label class="control-label col-sm-2" for="email">Name:</label> <div class="col-sm-10"> <input type="text" class="form-control" placeholder="Enter Name" name="name"> </div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="email">Email:</label> <div class="col-sm-10"> <input type="email" class="form-control" placeholder="Enter email" name="email"> </div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="pwd">Mobno:</label> <div class="col-sm-10"> <input type="number" class="form-control" placeholder="Enter Mobile No" name="mobno"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <input type="submit" class="btn btn-success" name="submit" value="Submit"> </div> </div> </form> </div> </body> </html>
Step 5 : Create View(edit.php):
<!DOCTYPE html> <html lang="en"> <head> <title>Ci Crud Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script> </head> <body> <h1 class="text-success" align="center">CI Crud Example</h1><br> <a href="<?php echo site_url();?>/crud" class="btn btn-primary pull-right">Back</a> <br><br> <h4 class="text-danger"><?php echo validation_errors(); ?></h4> <div class="container"> <form class="form-horizontal" method="post" action="<?php echo site_url();?>/crud/update"> <div class="form-group"> <label class="control-label col-sm-2" for="email">Name:</label> <div class="col-sm-10"> <input type="text" class="form-control" value="<?php echo $data->name;?>" name="name"> </div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="email">Email:</label> <div class="col-sm-10"> <input type="email" class="form-control" value="<?php echo $data->email;?>" name="email"> </div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="pwd">Mobno:</label> <div class="col-sm-10"> <input type="number" class="form-control" value="<?php echo $data->mobno;?>" name="mobno"> </div> </div> <input type="hidden" value="<?php echo $data->id;?>" name="id"> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <input type="submit" class="btn btn-success" name="submit" value="Submit"> </div> </div> </form> </div> </body> </html>