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

    The update() method is used to update an existing record in the database.

    Syntax:

    return $this->db->table('table_name')->where('condition')->set('array_of_objects')->update();

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

    $routes->add('user/list', 'User::list');
    $routes->get('user/edit/(:num)', 'User::save/$1');
    $routes->post('user/update', 'User::update');

    2. Create a list.php file in 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>Update</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/edit'.$row->id) ?>">Edit</a></td>";
            echo "</tr>"; $i++; } ?>
    </table>

    3. Create an edit.php file in the app/Views/ directory

    <html>
    <head>
    <title>Update user details</title>
    </head>
     
    <body>
     <?php
      if($result) {
      ?>
        <form method="post" action="<?php echo base_url('user/update'); ?>">
            <table width="600" border="1" cellspacing="5" cellpadding="5">
      <tr>
        <td width="230">Enter Your Name </td>
        <td width="329">
        <input type="hidden" name="txtId" value="<?php echo $result->id; ?>"/>
    <input type="text" name="txtFirstName" value="<?php echo $result->first_name; ?>"/></td>
      </tr>
      <tr>
        <td>Enter Your Email </td>
        <td><input type="text" name="txtLastName" value="<?php echo $result->last_name; ?>"/></td>
      </tr>
      <tr>
        <td>Enter Your Mobile </td>
        <td><input type="text" name="txtEmail" value="<?php echo $result->email; ?>"/></td>
      </tr>
      <tr>
        <td colspan="2" align="center">
        <input type="submit" name="update" value="Update"/></td>
      </tr>
    </table>
        </form>
        <?php } ?>
    </body>
    </html>

    4. 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 get_user($id) {
            return $this->db
                            ->table('user_info')
                            ->where(["id" => $id])
                            ->get()
                            ->getRow();
        }
    
        public function update($id, $data) {
            return $this->db
                            ->table('app_info')
                            ->where(["id" => $id])
                            ->set($data)
                            ->update();
        }
        
    }

    5. 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 edit($id) {
            $data['result'] = $this->userModel->get_user($id);
            echo view('edit', $data);
        }
    
        public function update() {
            $id     = $this->request->getPost('txtId');
            $first_name = $this->request->getPost('txtLastName');
            $last_name  = $this->request->getPost('txtLastName');
            $email      = $this->request->getPost('txtEmail');
            
            $data = [
                'first_name'        => $first_name,
                'last_name'     => $last_name,
                'email'         => $email,
            ];
    
            $result = $this->userModel->update($id, $data);
            if($result) {
                echo "User details are updated successfully.";
            } else {
                echo "Something went wrong";
            }
        }
    }