How to count previous month records in Codeigniter? For this issue, We will use 3 steps in CodeIgniter framework PHP.
- Create controler ie count_row.php
Path: CodeIgniter\application\controllers\ count_row.php
- Create Model ie record_fetch.php
Path: CodeIgniter\application\models\record_fetch.php
- Create View ie view_count_row.php
PAth: CodeIgniter\application\views\view_count_row.php
Create controlers: count_row.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Count_row extends CI_Controller {
public function __construct()
{
parent::__construct();
//calling model
$this->load->model("record_fetch");
}
public function index()
{
$this->data['countTotalRecords'] = $this->record_fetch->fetchRecord();
$this->load->view("view_count_row", $this->data);
}
}
?>
Create models: record_fetch.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Record_fetch extends CI_Model {
function __construct()
{
//call model constructor
parent::__construct();
}
function fetchRecord()
{
date_default_timezone_set('Asia/Kolkata');
$first_date = date('Y-m-d', strtotime('first day of last month'));
$last_date = date('Y-m-d', strtotime('last day of last month'));
$this->db->select('*');
$this->db->from('tableName');
$this->db->where('columnName BETWEEN "'.$first_date.'" AND "'.$last_date.'" ');
return $this->db->count_all_results();
}
}
?>
Create models: view_count_row.php
<!DOCTYPE html>
<html>
<body>
<?php
echo 'Total records found '.$countTotalRecords;
?>
</body>
</html>