This is easy way to finding last month records in codeigniter. For this, We will use three steps in CodeIgniter framework PHP.
1. Create controler ie total_row.php
Path: CodeIgniter\application\controllers\ total_row.php
2. Create Model ie record_fetch.php
Path: CodeIgniter\application\models\record_fetch.php
3. Create View ie view_total_row.php
PAth: CodeIgniter\application\views\view_total_row.php
Create controlers: total_row.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Total_row extends CI_Controller {
public function __construct()
{
parent::__construct();
//calling model
$this->load->model("record_fetch");
}
public function index()
{
$this->data['fetchTotalRecords'] = $this->record_fetch->fetchRecord();
$this->load->view("view_total_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.'" ');
$query = $this->db->get();
return $query->result();
}
}
?>
Create models: view_total_row.php
<!DOCTYPE html>
<html>
<body>
<table>
<thead>
<th>CustomerName</th>
<th>CustomerLocation</th>
</thead>
<tbody>
<?php
foreach($fetchTotalRecords as $rowFound)
{
$custName = $rowFound->cName;
$custLocation = $rowFound->cLocation;.
echo "<tr>
<td>$custName</td>
<td>$custLocation</td>
</tr>";
}
?>
</tbody>
</table>
</body>
</html>