How to Get Last 7 Days Data in CodeIgniter 4 Tutorial

Share this Article
Reading Time: 4 minutes
168 Views

Inside this article we will see how to get last 7 days data in codeigniter 4. Article has very classified information about last 7 days 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.

Last 7 days data selection is very helpful when we are creating data reports of application.

This article gives you a detailed on Codeigniter 4 to get Last 7 days data. Here, Creating a basic example of CodeIgniter from which we will select data in last 7 days.

Learn More –

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

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.


Turn Development Mode On

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

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.

CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.

# CI_ENVIRONMENT = production

 // Do it to 
 
CI_ENVIRONMENT = development

Now application is in development mode.


Database Connectivity to Application

Open .env file from project root. Search for DATABASE. You should see the connection environment variables.

Let’s set the value for those to connect with database.

 
#--------------------------------------------------------------------
# DATABASE
#-------------------------------------------------------------------- 
 database.default.hostname = localhost
 database.default.database = codeigniter4_app
 database.default.username = root
 database.default.password = root
 database.default.DBDriver = MySQLi
   

Now, database successfully connected with 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 all data from this table of last 7 days in CodeIgniter 4.


Create Query for Data

Open any controller from your application.

Let’s add last 7 days 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('name, email, phone_no');
        $usersTable->where("created_at >= DATE(NOW()) - INTERVAL 7 DAY");
        $query = $usersTable->get();

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

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

Query Concept

Make use of MySQL Last 7 Days Data Selection Concept.

Generated Query

SELECT `name`, `email`, `phone_no` FROM `users` WHERE `created_at` >= DATE(NOW()) - INTERVAL 7 DAY

When we run this code, we will get result as

We hope this article helped you to How to Get Last 7 Days 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