In this example we will discuss about how to show total amount month and year wise from database table using CodeIgniter framework PHP.
For show total amount month and year wise we use 3 file here
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Total_amount extends CI_Controller {
public function __construct()
{
parent::__construct();
/* calling model */
$this->load->model("Data_fetch", "a");
}
public function index()
{
$this->load->view("view_total_amount");
}
}
?>
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Data_fetch extends CI_Model {
function __construct()
{
/*call model constructor */
parent::__construct();
}
function fetchtable()
{
$query = $this->db->query('select year(date) as year, month(date) as month, sum(paid) as total_amount from amount_table group by year(date), month(date)');
return $query->result();
}
}
?>
<!DOCTYPE html>
<html>
<body>
<style>
table, td, th {
border: 1px solid #ddd;
text-align: left;
}
table {
border-collapse: collapse;
width: 40%;
}
th, td {
padding: 13px;
}
</style>
<table>
<thead>
<th>Year</th>
<th>Month</th>
<th>Total Amount</th>
</thead>
<tbody>
<?php
foreach($this->a->fetchtable() as $row)
{
$monthNum = $row->month;
$dateObj = DateTime::createFromFormat('!m', $monthNum);
$monthName = $dateObj->format('F');
/*name has to be same as in the database. */
echo "<tr>
<td>$row->year</td>
<td>$monthName</td>
<td>$row->total_amount</td>
</tr>";
}
?>
</tbody>
</table>
</body>
</html>
Now run the program on your browser with the below URL:
http://localhost/codeIgniter/index.php/Total_amount
This is my database table
After run the code i get the output
Year | Month | Total Amount |
---|---|---|
2016 | August | 150 |
2017 | August | 150 |
2017 | September | 300 |