Delete Multiple Rows using Checkbox in Codeigniter 3
In this section, we are going to learn the use of the CodeIgniter application to delete multiple rows using the checkbox. We can delete more than one row in Codeigniter by using jquery ajax. We always use our listing page to delete the button. But delete records will be very difficult for us if we add wrong data in some rows among the thousands of records in the database table. In this case, if we try to delete one by one row, it will take a lot of time and effort. If we want to make it easy, we can add or delete multiple records by the checkbox. This will help us to select checkboxes and delete records at a time.
In our following example, we are going to create an “Item” table, which will contain the title and description column. After that, we will enter some dummy records by running mysql query. Then we will add two routers. The first router will be created for listening items. The second router will be created for ajax requests. All the items will be displayed on the listening page with the checkbox. After that, we will put the code to remove the selected items. When the delete all button is clicked by the user, they will get ids of the selected checkbox and pass it in ajax methods. Then we will use these ids and delete all records in the ajax request. For this, we should follow step by step process, which is described as follows:
Step 1:
In this step, we will Create an item table. In order to do this, we will create one database, and then we will use our database and create a new table, “Item”. We can create the “Item” table by using the SQL query, which is described as follows:
Item Table:
After successfully creating the table, we will enter some dummy records.
Step 2:
In the second step, we will Add routers. For this, we will create new routers so that we can list items and send ajax requests. For this, we will open the router.php file and then add the code, which is shown below:
application/config/routes.php
Step 3:
In the third step, we will Create Item Controller. We will create this controller with deleteAll() and index(). For this, we will create an item.php file and then add the code, which is described as follows:
application/controllers/Item.php
Step 4:
In the fourth step, we will Create View file. In order to do this, item.php view file will be created. This file is used to show all items with checkbox. After that, we will write jqery ajax code, which is described as follows:
application/views/item.php
The above code is ready to run. After executing this code, we are able to see the following output: