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

    The model is responsible for handling the data of your application. Models receive input from Controllers and manage the application’s data, including inserting, updating, and retrieving information from the database.

    Key Responsibilities of a Model:

    • Managing Data: Models interact with the database by performing CRUD (Create, Read, Update, Delete) operations.
    • Interaction with Controllers: Models receive input from Controllers and process the data accordingly.

    Location of Models:

    Model classes are located in the application/models/ directory and can be organized into sub-directories.

     

    Creating a Basic Model in CodeIgniter 3

    Basic Syntax for Creating a Model

    The basic structure of a Model in CodeIgniter 3 is as follows:

    class Model_name extends CI_Model {
    
    }

     

    Rules:

    • The filename must match the class name and should start with an uppercase letter.
    • The class should extend the CI_Model class.

     

     

    Example: Creating a Blog Model

    Let’s walk through a practical example to understand how a Model works in CodeIgniter 3.

     

    class Blog_model extends CI_Model {
    
    	public function add() {
    		$data = [
    			‘title’		=> ‘Title’,
    			‘description’	=> ‘Description’
    		];
    		// insert(‘table_name‘, ‘array_of_object’)
    		$this->db->insert(‘blog_info’, $data);
    	}
    
    	public function get_blog_list() {
    		$query = $this->db->get(‘blo_info’);
    		return $query->result();
    	}
    }

     

    Explanation:

    1. Defining the Model: Blog_model is defined with methods add() and get_blog_list().
    2. Inserting Data: The add() method inserts a new record into the blog_info table.
    3. Fetching Data: The get_blog_list() method retrieves all records from the blog_info table.

     

    Loading a Model in CodeIgniter 3

    To use a Model in your Controller, you need to load it first. The load->model() method is used to load a Model in CodeIgniter 3.

    Basic Syntax:

    $this->load->model('model_name');

     

     

     

    Load a Model

    this->load->model() method is used to load a model.

    The basic syntax is as follows:

    $this->load->model('model_name');

     

    Loading a Model from a Sub-directory

    If your Model is inside a sub-directory, you need to specify the relative path from the models directory.

    For example, if your Model is located at application/models/user/Profile.php, you would load it as follows:

    $this->load->model('user/profile');

     

    Accessing Model Methods

    Once the Model is loaded, you can access its public methods using an object with the same name as the Model class:

    $this->load->model('model_name');
    $this->model_name->method();

     

    Example: Loading and Using a Model in a Controller

    Here’s an example of a Controller that loads the Blog_model and uses its methods:

    class Blog extends CI_Controller {
    
    	public function index() {
        	$this->load->model(‘blog_model’);
    
            $data[‘list’] = $this->blog_model->get_blog_list();
    
            $this->load->view('about_us', $data);
       	}
    }

     

    Explanation:

    1. Loading the Model: Blog_model is loaded using $this->load->model('blog_model');.
    2. Using Model Methods: The get_blog_list() method is called to fetch the list of blogs.
    3. Passing Data to the View: The fetched data is passed to the about_us view.