codeigniter
  1. codeigniter-delete-multiple-rows-using-checkbox-in-codeigniter3

Delete Multiple Rows using Checkbox in Codeigniter 3

Deleting multiple rows using checkboxes in CodeIgniter 3 is a common feature in applications that manages large amounts of data. This feature allows users to select multiple rows, and then delete them all at once. In this tutorial, we'll show you how to implement this functionality in CodeIgniter 3.

Syntax

The syntax for deleting multiple rows using checkboxes in CodeIgniter 3 is as follows:

$this->db->where_in('id', $ids_to_delete);
$this->db->delete('table_name');

Example

Consider a table called employees with the following fields:

  • id: the employee's ID
  • name: the employee's name
  • department: the employee's department

To delete multiple rows using checkboxes, you would need to create a form with a checkbox for each row. Here's an example of what the form might look like:

<form action="<?= base_url('controller/delete_multiple'); ?>" method="post">
  <table>
    <thead>
      <tr>
        <th>Select</th>
        <th>ID</th>
        <th>Name</th>
        <th>Department</th>
      </tr>
    </thead>
    <tbody>
      <?php foreach ($employees as $employee) : ?>
        <tr>
          <td><input type="checkbox" name="ids_to_delete[]" value="<?= $employee->id; ?>"></td>
          <td><?= $employee->id; ?></td>
          <td><?= $employee->name; ?></td>
          <td><?= $employee->department; ?></td>
        </tr>
      <?php endforeach; ?>
    </tbody>
  </table>
  <input type="submit" value="Delete Selected" />
</form>

When the user selects one or more checkboxes and submits the form, the following method will be called to delete the selected rows:

public function delete_multiple()
{
    // Get the IDs of the rows to delete
    $ids_to_delete = $this->input->post('ids_to_delete');

    // Check if any rows were selected
    if (!empty($ids_to_delete)) {
        // Delete the selected rows
        $this->db->where_in('id', $ids_to_delete);
        $this->db->delete('employees');
    }

    // Redirect to the previous page
    redirect('previous_page');
}

Explanation

In the example above, we first get the IDs of the rows to delete from the ids_to_delete array that was submitted with the form. We then check if any rows were selected by checking if the ids_to_delete array is not empty. If at least one row was selected, we use the where_in() method to filter the employee's table by the IDs to delete, and then call the delete() method to delete all the matching rows.

Use

Deleting multiple rows using checkboxes is a common feature in applications that manage large amounts of data. This feature allows users to select multiple rows and delete them all at once, making it easier and more efficient to manage data.

Important Points

  • You should always validate user input before deleting any data from the database.
  • Make sure to use CodeIgniter's built-in query builder to properly escape and sanitize input values.
  • Always redirect to a previous page after executing a delete operation to prevent unintended actions by the user.

Summary

In this tutorial, we showed you how to delete multiple rows using checkboxes in CodeIgniter 3. We started by creating a form with a checkbox for each row, and then processed the form submission using a controller method. We then used CodeIgniter's built-in query builder to delete the selected rows from the database, and finally discussed some important points to keep in mind when implementing this functionality in your CodeIgniter application.

Published on: