How to Get Month Wise Data in CodeIgniter 4 Tutorial

Share this Article
Reading Time: 4 minutes
1,281 Views

Inside this article we will see how to get month wise data in codeigniter 4. Article has very classified information about month wise data selection from database in codeigniter 4.

Selecting data rows from database in codeigniter 4, we have several options available as – Using Models, Using Query Builder, Using raw queries.

Monthwise data selection is very helpful when we are creating data reports of application.

This article gives you a detailed on Codeigniter 4 to get month wise data. Here, Creating a basic example of CodeIgniter from which we will sum number of users month wise.

Learn More –

Let’s get started.


Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system.

To set application we have multiple options to proceed.

Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer –

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


.env Setup

When we install CodeIgniter 4, we will have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Either we can do via renaming file as simple as that. Also we can do by terminal command.

Open project in terminal

$ cp env .env

Above command will create a copy of env file to .env file.

Now we are ready to use environment variables.


Enable Development Mode

CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.

Open .env file from root.

# CI_ENVIRONMENT = production

 // Do it to 
 
CI_ENVIRONMENT = development

Now application is in development mode.


Database Connection

Open .env file from project root.

Search for DATABASE. You should see the connection environment variables. Give your updated details of database connection string.

 
#--------------------------------------------------------------------
# DATABASE
#--------------------------------------------------------------------

database.default.hostname = localhost
database.default.database = codeigniter4_app
database.default.username = root
database.default.password = root
database.default.DBDriver = MySQLi
database.default.DBPrefix =
   

Now, database successfully connected with the application.


Sample Database Data Rows

Here, we have taken a sample database to understand this example. codeigniter4_app is the name of database.

Let’s say we have a users table inside it which contains users data. Please look into this screenshot to get clear vision about table.

Next,

We want to get the count of users month wise from this table in CodeIgniter 4.


Create Query for Data

Open any controller from your application.

Let’s add month wise concept into it to get data.

<?php

namespace App\Controllers;

class Site extends BaseController
{
    private $db;
  
    public function __construct()
    {
        $this->db = db_connect();
    }

    public function getUsers()
    {
        $usersTable = $this->db->table("users");
        $usersTable->select('count(id) as total_users, Month(created_at) as month');
        $usersTable->groupBy("Month(created_at)");
        $query = $usersTable->get();

        //echo $this->db->getLastQuery();

        echo "<pre>";
        print_r($query->getResult());
    }
}

Query Concept

Make use of Query grouping month wise to get total users count month wise.

Generated Query

SELECT count(id) as total_users, Month(created_at) as month FROM `users` GROUP BY Month(created_at)

When we run this code, we will get result as

We hope this article helped you to How to Get Month Wise Data in CodeIgniter 4 Tutorial in a very detailed way.

Buy Me a Coffee

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on CodeIgniter 4 here

Learn Web Development Courses @ $4 only.
Web Development Courses @ $4