This block of code of will explains about how to delete data from table using CodeIgniter.
Which involves mainly three steps.
View
demodata.php
<html>
<head>
<style>
.container {
width: 500px;
height: 100px;
padding: 200px;
}
table, th, td {
border: 1px solid black;
width:500px;
}
</style>
</head>
<body class="container">
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Number</th>
<th>Update</th>
</tr>
<?php
foreach ($k->result() as $row) {
echo '<tr>';
echo '<td>' . $row->id . '</td>';
echo '<td>' . $row->name . '</td>';
echo '<td>' . $row->number . '</td>';
echo "<td><a href='updatedemodata?id=$row->id'>Update</a>
<a href='deletedemodata?id=$row->id'> / Delete</a></td>";
}
?>
</table>
</body>
</html>
Controller
Welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function deletedemodata(){
if(isset($_GET['id'])) {
if (($_GET['id'] !== '')) {
$id = $_GET['id'];
$this->levels->deletedemodata($id);
} else {
echo 'error while deleting';
}
}
}
}
?>
Model
level.php
<?php
class Levels extends CI_Model
{
public function deletedemodata($id)
{
$this->load->database();
$this->db->where('id', $id);
$this->db->delete('demo');
echo 'Data Deleted successfully';
$this->load->helper('url');
redirect('/welcome/demodata');
}
}
?>
Which involves mainly three steps.
- View
- Controller
- Model
View
demodata.php
<html>
<head>
<style>
.container {
width: 500px;
height: 100px;
padding: 200px;
}
table, th, td {
border: 1px solid black;
width:500px;
}
</style>
</head>
<body class="container">
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Number</th>
<th>Update</th>
</tr>
<?php
foreach ($k->result() as $row) {
echo '<tr>';
echo '<td>' . $row->id . '</td>';
echo '<td>' . $row->name . '</td>';
echo '<td>' . $row->number . '</td>';
echo "<td><a href='updatedemodata?id=$row->id'>Update</a>
<a href='deletedemodata?id=$row->id'> / Delete</a></td>";
}
?>
</table>
</body>
</html>
Controller
Welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function deletedemodata(){
if(isset($_GET['id'])) {
if (($_GET['id'] !== '')) {
$id = $_GET['id'];
$this->levels->deletedemodata($id);
} else {
echo 'error while deleting';
}
}
}
}
?>
Model
level.php
<?php
class Levels extends CI_Model
{
public function deletedemodata($id)
{
$this->load->database();
$this->db->where('id', $id);
$this->db->delete('demo');
echo 'Data Deleted successfully';
$this->load->helper('url');
redirect('/welcome/demodata');
}
}
?>