Course Content
Introduction to CodeIgniter 4
CodeIgniter is an Application Development Framework. CodeIgniter is a popular and powerful MVC (Model-View-Controller) framework that is used to develop web applications. It is a free and Open-source PHP framework.
0/5
MVC (Model-View-Controller)
MVC stands for Model-View-Controller. MVC is an application design model consisting of three interconnected parts. They include the model (data), the view (user interface), and the controller (processes that handle input).
0/6
Sessions
The Session class allows you to maintain a user’s "state" and track their activity while they browse your site.
0/1
URI Routing
There is a one-to-one relationship between a URL string and its corresponding controller class/method.
0/2
Working with Database
Like any other framework, we need to interact with the database very often and CodeIgniter makes this job easy for us. It provides a rich set of functionalities to interact with the database.
0/5
Spreadsheet
PhpSpreadsheet is a PHP library for reading and writing spreadsheet files. Importing Excel and CSV into MySQL help to save the user time and avoid repetitive work.
0/1
CodeIgniter 4
About Lesson

A delete() method is used to delete data from the database.

Syntax:

$this->db->table('table_name')->where('condition')->delete();

1. Copy and Paste the following lines in app/Config/Routes.php

$routes->add('user/list', 'User::list');
$routes->get('user/delete/(:num)', 'User::delete/$1');

2. Create a list.php file in the app/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>
    </tr>
    <?php $i=1; foreach($result as $row) { 
        echo "<tr>"; 
        echo "<td>".$i. "</td>"; 
        echo "<td>".$row->first_name."</td>";
        echo "<td>".$row->last_name."</td>"; 
        echo "<td>".$row->email."</td>"; 
        echo "<td><a href="<?php echo base_url('user/delete'.$row->id) ?>">Delete</a></td>";
        echo "</tr>"; $i++; } ?>
</table>

3. Create a model file UserModel.php in the app/Models/ directory.

<?php 
namespace App\Models;

use CodeIgniter\Model;
use CodeIgniter\Database\ConnectionInterface;

class UserModel extends Model {
    
    protected $db;
    public function __construct(ConnectionInterface &$db) {
        $this->db =& $db;
    }

    public function list() {
        return $this->db
                        ->table('user_info')
                        ->get()
                        ->getResult();
    }

    public function delete($id) {
        return $this->db
                        ->table('user_info')
                        ->where(["id" => $id])
                        ->delete();
    }
    
}

4. Create a controller file User.php in the app/Controllers/ directory.

<?php
namespace App\Controllers;

use App\Models\UserModel;

class User extends BaseController {

    public function __construct() {

        $db = db_connect();
        $this->userModel = new UserModel($db);
    }

    public function add() {
        $this->load->view('add');
    }

    public function list() {
        $data['result'] = $this->userModel->list();
        echo view('list', $data);
    }

    public function delete($id) {
        $result = $this->userModel->delete($id);
        if($result) {
            echo "User record is deleted successfully.";
        } else {
            echo "Something went wrong";
        }

    }
}