Tutorial Part 2 CRUD using Codeigniter and Bootstrap (Delete in array using checkbox)

well after first tutorial here i will continue taht tutorial about CRUD today i will make about delete using checkbox and let change view to make edit and delete here is whole script view

  <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8;charset=utf-8" />
 <meta name="robots" content="index, follow" />
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <!-- Bootstrap -->
 <link href="//cdn.datatables.net/plug-ins/3cfcc339e89/integration/bootstrap/3/dataTables.bootstrap.css" rel="stylesheet" media="screen"> 

 <!-- Bootstrap -->
 <link href="<?php echo base_url()?>asset/css/bootstrap.min.css" rel="stylesheet" media="screen">
 <link href="<?php echo base_url()?>asset/css/bootstrap-theme.min.css" rel="stylesheet" media="screen">
 <link href="<?php echo base_url()?>asset/css/style.css" rel="stylesheet" media="screen">
 <link href="<?php echo base_url()?>asset/css/costum/flexslider.css" rel="stylesheet" media="screen">
 <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
 <script src="//cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js"></script>
 <script src="//cdn.datatables.net/plug-ins/3cfcc339e89/integration/bootstrap/3/dataTables.bootstrap.js"></script>
 <script type="text/javascript">
 $(document).ready(function() {
 $('#example').dataTable();
 } );
 </script>
 </head>
 <body>
 
 <form name="indonesia" action="<?php echo site_url('admin/karir/delete_multiple'); ?>" method="post"> 
 
 <button type="submit" class="btn btn-danger" name="hapus" value="hapus">Hapus</button>
 
 <?php echo anchor('admin/karir/tambah', 'Tambah Karir');?>
 <table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
 <thead>
 <tr>
 <th>
 <button type="button" class="btn btn-info" onClick="check_all()" >Check</button>
 <button type="button" class="btn btn-success" onClick="uncheck_all()" >Un-Check</button>
 </th>
 <th>Id</th>
 <th>Judul</th>
 <th>Status</th>
 </tr>
 </thead>
 
 <tbody>
 <?php
 foreach ($ListKarir->result() as $row)
 {
 ?>
 <tr>
 <td><input type="checkbox" name="item[]" id="item[]" value="<?=$row->id_project ?>"></td>
 <td><?=$row->id_project ?></td>
 <td><?=$row->judul ?></td>
 <td><?=$row->status ?></td>
 </tr>
 <?php } ?>
 </tbody>
 </table>
 
 </form> 

 <!-- javascript check and check all -->
<script type="text/javascript">
$(document).ready(function() {
 $('#example').dataTable();
} );
</script>
<script language="javascript">
function check_all()
{
 var chk = document.getElementsByName('item[]');
 for (i = 0; i < chk.length; i++)
 chk[i].checked = true ;
}
 
function uncheck_all()
{
 var chk = document.getElementsByName('item[]');
 for (i = 0; i < chk.length; i++)
 chk[i].checked = false ;
}
</script>
 </body>
</html>
  and then you should make controller for it, here is controller

  • <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Karir extends CI_Controller {
     public function index()
     {
     // here is data from db which get from model
     $data['ListKarir'] = $this->karir_model->getAllKarir();
    
     //here is view with data parsing from model
     $this->load->view("admin/karir", $data);
     }
    
     //Proses it to model
     public function delete_multiple()
     {
     $this->karir_model->remove_checked_indonesia();
     redirect('admin/karir');
     }

and the last we create model

 

<?php
 class Karir_model extends CI_Model
 {
 function __construct()
 {
 parent::__construct();
 }
 function getAllKarir()
 {
 $this->db->from("tabel_project");
 return $this->db->get();
 }
 
 function remove_checked_indonesia() 
 {
 $action = $this->input->post('hapus');
 if ($action) 
 {
 $delete = $this->input->post('item');
 for ($i=0; $i < count($delete) ; $i++) 
 { 
 $this->db->where('id_project', $delete[$i]);
 $this->db->delete('tabel_project');
 }
 }
 }

and your interface will be look like this, if any question please be nice to comment here

 

check