CRUD (Create, Read , Update, Delete) Using CodeIgniter and Bootstrap

 

In this article I will discuss how to make the crud operations using codeigniter and bootstrap. Crud stands for create, read, update and delete are used to create the basis of a database application. CodeIgniter is a framework built using php and as adherents MVC concept that allows a person to develop web applications. Bootstrap is a ui or design templates provided by twitter to build web templates with a fast and reliable.

CRUD ( Create, Read , Update, Delete ) Using CodeIgniter and Bootstrap

To make this crud required codeigniter and bootstrap which you can download on their official website. After that, extracts the files and folders into a new folder that is in the web root of our computer. after that, create a new database and table name with the name mydata crud or up to you. For SQL syntax as below.

--
-- Database: `mydata`
--

CREATE TABLE IF NOT EXISTS `crud` (
  `idcrud` int(11) NOT NULL AUTO_INCREMENT,
  `firstname` varchar(45) NOT NULL,
  `lastname` varchar(45) NOT NULL,
  `age` varchar(20) NOT NULL,
  `address` text NOT NULL,
  PRIMARY KEY (`idcrud`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

INSERT INTO `crud` (`idcrud`, `firstname`, `lastname`, `age`, `address`) VALUES
(1, 'ghazali', 'samudera', '22', 'desa baroh'),
(2, 'bukhari', 'zulkifli', '20', 'desa baroh'),
(6, 'resa', 'kira', '34', 'keno'),
(7, 'Boyhaki', 'Munir', '24', 'kembang tanjong'),
(8, 'Murad', 'Yaa', '23', 'kembang tanjong');

Open the file /var/www/html/main/new folder/application/config/autoload.php and edit as below.

   
$autoload['libraries'] = array('database');
$autoload['helper'] = array('url', 'form', 'html');
$autoload['model'] = array('mcrud');

Open the file /var/www/html/main/new folder/application/config/config.php and edit as below.
   
$config['base_url'] = 'http://localhost/main/new folder/';

Open the file /var/www/html/main/new folder/application/config/database.php and edit as below.
   
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'pidie';
$db['default']['database'] = 'mydata';
$db['default']['dbdriver'] = 'mysql';

Create a file /var/www/html/main/new folder/application/models/mcrud and syntax as below.

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

class Mcrud extends CI_Model {
 
 function add() {
  $fn = $this->input->post('fn');
  $ln = $this->input->post('ln');
  $ag = $this->input->post('ag');
  $ad = $this->input->post('ad');
  $data = array(
   'firstname' => $fn,
   'lastname' => $ln,
   'age' => $ag,
   'address' => $ad
  );
  $this->db->insert('crud', $data);
 }
 function view() {
  $ambil = $this->db->get('crud');
  if ($ambil->num_rows() > 0) {
   foreach ($ambil->result() as $data) {
    $hasil[] = $data;
   }
   return $hasil;
  }
 }
 function edit($a) {
  $d = $this->db->get_where('crud', array('idcrud' => $a))->row();
  return $d;
 }
 function delete($a) {
  $this->db->delete('crud', array('idcrud' => $a));
  return;
 }
 function update($id) {
  $fn = $this->input->post('fn');
  $ln = $this->input->post('ln');
  $ag = $this->input->post('ag');
  $ad = $this->input->post('ad');
  $data = array(
   'firstname' => $fn,
   'lastname' => $ln,
   'age' => $ag,
   'address' => $ad
  );
  $this->db->where('idcrud', $id);
  $this->db->update('crud', $data);
 }
}

Create a file /var/www/html/main/new folder/application/controllers/ccrud and syntax as below.

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

class Ccrud extends CI_Controller {

 public function index()
 {
  $data['data_get'] = $this->mcrud->view();
  $this->load->view('header');
  $this->load->view('vcrud', $data);
  $this->load->view('footer');
 }
 function add() {
  $this->load->view('header');
  $this->load->view('vcrudnew');
  $this->load->view('footer');
 }
 function edit() {
  $kd = $this->uri->segment(3);
  if ($kd == NULL) {
   redirect('ccrud');
  }
  $dt = $this->mcrud->edit($kd);
  $data['fn'] = $dt->firstname;
  $data['ln'] = $dt->lastname;
  $data['ag'] = $dt->age;
  $data['ad'] = $dt->address;
  $data['id'] = $kd;
  $this->load->view('header');
  $this->load->view('vcrudedit', $data);
  $this->load->view('footer');
 }
 function delete() {
  $u = $this->uri->segment(3);
  $this->mcrud->delete($u);
  redirect('ccrud');
 }
 function save() {
  if ($this->input->post('mit')) {
   $this->mcrud->add();
   redirect('ccrud');
  } else{
   redirect('ccrud/tambah');
  }
 }
 function update() {
  if ($this->input->post('mit')) {
   $id = $this->input->post('id');
   $this->mcrud->update($id);
   redirect('ccrud');
  } else{
   redirect('ccrud/edit/'.$id);
  }
 }
}

/* End of file welcome.php */
/* Location: ./application/controllers/ccrud.php */

Create a file /var/www/html/main/new folder/application/views/header.php and syntax as below.

   
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>CRUD Data</title>

    <!-- Bootstrap -->
    <link href="<?php echo base_url() ?>bootstrap/css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="<?php echo base_url() ?>bootstrap/js/html5shiv.min.js"></script>
      <script src="<?php echo base_url() ?>bootstrap/js/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
<p>
</p>
    <div class="container">

Create a file /var/www/html/main/new folder/application/views/footer.php and syntax as below.
   
    </div>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="<?php echo base_url() ?>bootstrap/js/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="<?php echo base_url() ?>bootstrap/js/bootstrap.min.js"></script>
  </body>
</html>

Create a file /var/www/html/main/new folder/application/views/vcrud.php and syntax as below.
   
<p>
<a href="<?php echo site_url('ccrud/add') ?>" class="btn btn-primary">Add New</a>
</p>
<div class="table-responsive">
   <table class="table table-bordered table-hover table-striped">
      <caption>List Data</caption>
      <thead>
        <tr>
          <th width="80px">ID</th>
          <th>First Name</th>
          <th>Last Name</th>
          <th>Age</th>
          <th>Address</th>
          <th width="80px">Action</th>
        </tr>
      </thead>
      <tbody>
      <?php
  if ($data_get == NULL) {
  ?>
  <div class="alert alert-info" role="alert">Data masih kosong, tolong di isi!</div>
  <?php
  } else {
  foreach ($data_get as $row) {
  ?>
        <tr>
          <td><?php echo $row->idcrud; ?></td>
          <td><?php echo $row->firstname; ?></td>
          <td><?php echo $row->lastname; ?></td>
          <td><?php echo $row->age; ?></td>
          <td><?php echo $row->address; ?></td>
          <td>
           <a href="<?php echo site_url('ccrud/edit/' . $row->idcrud); ?>" class="btn btn-warning btn-xs"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a>
           <a href="<?php echo site_url('ccrud/delete/' . $row->idcrud); ?>" class="btn btn-danger btn-xs"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></a>
          </td>
      <?php
      }
  }
      ?>
        </tr>
      </tbody>
   </table>
</div>

Create a file /var/www/html/main/new folder/application/views/vcrudnew.php and syntax as below.

<?php echo form_open('ccrud/save', 'role="form"'); ?>
  <div class="form-group">
    <label for="fn">First Name</label>
    <input type="text" class="form-control" id="fn" name="fn">
  </div>
  <div class="form-group">
    <label for="fn">Last Name</label>
    <input type="text" class="form-control" id="ln" name="ln">
  </div>
  <div class="form-group">
    <label for="ag">Age</label>
    <input type="text" class="form-control" id="ag" name="ag">
  </div>
  <div class="form-group">
    <label for="ad">Address</label>
    <input type="text" class="form-control" id="ad" name="ad">
  </div>
  <input type="submit" name="mit" class="btn btn-primary" value="Submit">
  <button type="button" onclick="location.href='<?php echo site_url('ccrud') ?>'" class="btn btn-success">Back</button>
</form>
<?php echo form_close(); ?>

Create a file /var/www/html/main/new folder/application/views/vcrudedit.php and syntax as below.

<?php echo form_open('ccrud/update', 'role="form"'); ?>
  <div class="form-group">
    <label for="fn">First Name</label>
    <input type="text" class="form-control" id="fn" name="fn" value="<?php echo $fn ?>">
  </div>
  <div class="form-group">
    <label for="fn">Last Name</label>
    <input type="text" class="form-control" id="ln" name="ln" value="<?php echo $ln ?>">
  </div>
  <div class="form-group">
    <label for="ag">Age</label>
    <input type="text" class="form-control" id="ag" name="ag" value="<?php echo $ag ?>">
  </div>
  <div class="form-group">
    <label for="ad">Address</label>
    <input type="text" class="form-control" id="ad" name="ad" value="<?php echo $ad ?>">
  </div>
  <input type="hidden" name="id" value="<?php echo $id ?>" />
  <input type="submit" name="mit" class="btn btn-primary" value="Update">
  <button type="button" onclick="location.href='<?php echo site_url('ccrud') ?>'" class="btn btn-success">Back</button>
</form>
<?php echo form_close(); ?>

Done, run in your browser such as Mozilla Firefox, Google Chrome, Opera web browser, Apple Safari and Microsoft Internet Explorer.
Powered by Blogger.