CodeIgniter Tutorial
Home
CodeIgniter Installation
CodeIgniter Configuration
CodeIgniter Features
CodeIgniter URL
CodeIgniter Model
CodeIgniter View
CodeIgniter Controller
CodeIgniter Helper
CodeIgniter Library
CodeIgniter Session
CodeIgniter Flashdata
CodeIgniter Tempdata
CodeIgniter Example
CodeIgniter File Uploading
Form Validation
CodeIgniter Insert Data
CodeIgniter Display Data
CodeIgniter Delete Data
CodeIgniter Update Data
CodeIgniter Send Email
How to delete data in codeigniter
Create a view file display.php and save the below code it in application/views/display.php.
<!DOCTYPE html> <html lang="en"> <head> <title>Display Data</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <table class="table table-hover"> <thead> <tr> <th>Id</th> <th>Name</th> <th>Email</th> <th>Delete</th> </tr> </thead> <tbody> <?php foreach($data as $d){ ?> <tr> <td><?php echo $d->id; ?></td> <td><?php echo $d->name; ?></td> <td><?php echo $d->email; ?></td> <td><a href="delete?id=<?= $d->id;?>">Delete</a></td> </tr> <?php } ?> </tbody> </table> </div> </body> </html>
Create a controller file Demo.php and save it in application/controller/Demo.php.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Demo extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model("Demo_Model"); } function insert(){ $this->load->view("insert"); } function save(){ $data = $this->input->post(); $result = $this->Demo_Model->save("demo",$data); if (!empty($result)){ redirect(base_url("demo/insert")); } } function display(){ $data["data"] = $this->Demo_Model->get_all("demo"); $this->load->view("display",$data); } function delete(){ $id = $this->input->get("id"); $this->Demo_Model->delete("demo",array("id" => $id)); } }
Create a model file Demo.php and save it in application/models/Demo_Model.php.
<?php class Demo_Model extends CI_Model{ function save($table_name,$data){ $this->db->insert($table_name,$data); return $this->db->insert_id(); } function get_all($table_name){ $this->db->select("*"); $this->db->from($table_name); $query = $this->db->get(); return $query->result(); } function delete($table_name,$where){ $this->db->where($where); $this->db->delete($table_name); redirect("demo/display"); } }
Let us execute this example by the following URL in the browser. This URL may be different based on your website.
https://howtowebcode.com/demo/display