A delete() method is used to delete data from the database.
The DELETE statement is used to delete records from a table:
DELETE FROM table_name WHERE some_column = some_value
Note: The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted!
1. Create a list.php file in applications/views/ directory
<table width="600" border="1" cellspacing="5" cellpadding="5"> <tr style="background:#CCC"> <th>Sr No</th> <th>First_name</th> <th>Last_name</th> <th>Email Id</th> <th>Delete</th> <th>Update</th> </tr> <?php $i=1;
foreach($result as $key => $row) { echo "<tr>"; echo "<td>".++$key. "</td>"; echo "<td>".$row->first_name."</td>"; echo "<td>".$row->last_name."</td>"; echo "<td>".$row->email."</td>"; echo "</tr>"; echo "<td><a href="<?php echo base_url('user/delete/'.$row->id); ?>"> Delete</a></td>";
$i++;
} ?> </table>
2. Create a model file User_model.php in the applications/models/ directory.
class User_model extends CI_Model {
/*Select*/
function list() {
$this->db->select([“*”]);
$this->db->from(‘user_info’);
$query = $this->db->get();
return $query->result();
}
public function delete($id) {
$where = [“id” => $id];
return $this->db->delete(‘user_info’, $where);
}
}3. Create a controller file User.php in the applications/controllers/ directory.
class User extends CI_Controller {
public function __construct() {
/*call CodeIgniter's default Constructor*/
parent::__construct();
/*load model*/
$this->load->model('User_model');
}
public function list() {
$data[‘result’] = $this->user_model->list();
$this->load->view(‘list’, $data);
}
public function delete($id) {
$result = $this->user_model->delete($id);
if($result) {
echo “User record is deleted successfully.”;
} else {
echo “Something went wrong”;
}
}
}