Course Content
Introduction to CodeIgniter
CodeIgniter is a powerful PHP framework built for developers who need a simple and elegant toolkit to create full-featured web applications.
0/3
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. The segments in a URI normally follow this pattern:
0/1
Forms and Input
Forms provide a way for users to interact with the application and submit data.
0/1
Composer
Composer is dependency manager in PHP. it allows you to declare the libraries your project depends on and it will manage (install/update) them for you.
0/1
Security
You can enable CSRF protection by modifying your application/config/config.php file
0/1
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
DataTable
DataTables is a table enhancing plug-in for the jQuery Javascript library that helps in adding sorting, paging, and filtering abilities to plain HTML tables with minimal effort. The main goal is to enhance the accessibility of data in normal HTML tables.
0/1
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
Payment Gateway
Razorpay and PayTM Payment Gateway
0/2
Chatbot
WhatsApp Chatbot and Telegram Chatbot
0/2
CodeIgniter 3
    About Lesson

    In CodeIgniter, the result() and row() methods are commonly used to retrieve data from a database:

    • result(): Returns a list of data objects (multiple rows).
    • row(): Returns a single row as an object.

    Basic Syntax to Retrieve Data from MySQL

    SELECT column_name(s) FROM table_name

    Or, to retrieve all columns from a table:

    SELECT * FROM table_name

     

    Step-by-Step Implementation

    Below, we’ll create a simple example demonstrating how to retrieve data from a database and display it in a table format.

     

    File Structure

    application/

    ├── controllers/
    │ └── User.php

    ├── models/
    │ └── User_model.php

    └── views/
    └── list.php

     

    Step 1: Create list.php in application/views/

    The list.php view file will display the fetched data in a table format.

    File: application/views/list.php

     

    <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 $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 "</tr>"; $i++; } ?>
    </table>

    Explanation

    • The table displays a list of users fetched from the database.
    • Each row contains the user’s details along with “Delete” and “Update” options linked to the respective methods in the controller.

     

    Step 2: Create User_model.php in application/models/

    The User_model handles fetching data from the database using CodeIgniter’s Query Builder.

    File: application/models/User_model.php

     

    class User_model extends CI_Model {
    	/*Select*/
    	function list() {
    		$this->db->select([“*”]);
    		$this->db->from(‘user_info’);
    		$query = $this->db->get();
    		return $query->result();
    	}
    	
    }

     

    Explanation

    • $this->db->select('*'): Select all columns from the table.
    • $this->db->from('user_info'): Specifies the table from which to fetch data.
    • $this->db->get(): Executes the query.
    • return $query->result(): Returns the result set as an array of objects.

     

    Step 3: Create User.php in application/controllers/

    The User controller manages the flow between the model and the views.

    File: application/controllers/User.php

     

    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);
    	}
    }

     

    Explanation

    • __construct(): Loads the User_model when the controller is instantiated.
    • list(): Calls the list() method from the User_model to fetch data and pass it to the list.php view.

     

    Summary

    • Views: Created list.php to display a table of users with options to delete or update.
    • Model: Created User_model.php to interact with the database and fetch records using result().
    • Controller: Created User.php to handle user requests, fetch data using the model, and load the views.

    Key Points

    • result() is used when you want to fetch multiple rows from a table.
    • row() is used when you need to fetch a single row from the result set (not used in this tutorial but can be implemented similarly).