In every listing pages provides delete button . Every individual delete button we have to delete single records. But it is time consum...

Delete multiple Records using Checkbox using Codeigniter

In every listing pages provides delete button . Every individual delete button we have to delete single records. But it is time consuming when we have large number of records and we want to multiple records delete.

Here, i share code for making how to delete multiple records.

1) Create Table
CREATE TABLE IF NOT EXISTS `items` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `description` text COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=16;

2) Add Routes in route.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['item'] = "item";
$route['itemDelete']['post'] = "item/deleteAll";

3) Create Controller: 
<?php
defined('BASEPATH') OR exit('No direct script access allowed'); 
class Item extends CI_Controller {    
    public function __construct() {
       parent::__construct();
       $this->load->database();
    } 
    public function index()
    {
        $data['data'] = $this->db->get("items")->result();
        $this->load->view('item', $data);
    }
    public function deleteAll()
    {
        $ids = $this->input->post('ids'); 
        $this->db->where_in('id', explode(",", $ids));
        $this->db->delete('items'); 
        echo json_encode(['success'=>"Item Deleted successfully."]);
    } 
}

4) Create View
<html>
<head>
    <title>how to delete multiple records using checkbox in codeigniter - itsolutionstuff.com</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" /> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
    <div class="col-lg-12 margin-tb">
        <div class="pull-left">
            <h2>how to delete multiple records using checkbox in codeigniter - itsolutionstuff.com</h2>
        </div>
    </div>
</div> 
<button style="margin-bottom: 10px" class="btn btn-primary delete_all" data-url="/itemDelete">Delete All Selected</button> 
<table class="table table-bordered" style="margin-top:20px"> 
  <thead>
      <tr>
          <th width="50px"><input type="checkbox" id="master"></th>
          <th>Title</th>
          <th>Description</th>
      </tr>
  </thead> 
  <tbody>
   <?php foreach ($data as $item) { ?>      
      <tr>
          <td><input type="checkbox" class="sub_chk" data-id="<?php echo $item->id; ?>"></td>
          <td><?php echo $item->title; ?></td>
          <td><?php echo $item->description; ?></td>
      </tr>
   <?php } ?>
  </tbody> 
</table>
</div> 
<script type="text/javascript">
    $(document).ready(function () { 
        $('#master').on('click', function(e) {
         if($(this).is(':checked',true))  
         {
            $(".sub_chk").prop('checked', true);  
         } else {  
            $(".sub_chk").prop('checked',false);  
         }  
        });
        $('.delete_all').on('click', function(e) {
            var allVals = [];  
            $(".sub_chk:checked").each(function() {  
                allVals.push($(this).attr('data-id'));
            });   
            if(allVals.length <=0)  
            {  
                alert("Please select row.");  
            }  else {  
                var check = confirm("Are you sure you want to delete this row?");  
                if(check == true){   
                    var join_selected_values = allVals.join(",");  
                    $.ajax({
                        url: $(this).data('url'),
                        type: 'POST',
                        data: 'ids='+join_selected_values,
                        success: function (data) {
                          console.log(data);
                          $(".sub_chk:checked").each(function() {  
                              $(this).parents("tr").remove();
                          });
                          alert("Item Deleted successfully.");
                        },
                        error: function (data) {
                            alert(data.responseText);
                        }
                    }); 
                  $.each(allVals, function( index, value ) {
                      $('table tr').filter("[data-row-id='" + value + "']").remove();
                  });
                }  
            }  
        });
    });
</script> 
</body>
</html>

5) Create Model:
public function DeleteById($id){
       $this->db->where('id', $id);
     $this->db->delete('guestbook');
     return $this->db->affected_rows();
  } 

6) Output:



Fruxinfo Pvt. Ltd

0 coment�rios: