CodeIgniter Laravel PHP Example Javascript jQuery MORE Videos New

Show total amount month and year wise CodeIgniter framework PHP


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

  1. total_amount.php (CodeIgniter\application\controllers\total_amount.php )
  2. data_fetch.php (CodeIgniter\application\models\data_fetch.php)
  3. view_total_amount.php (CodeIgniter\application\vie\view_total_amount.php)

total_amount.php

<?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"); 
    }  
}  
?>  

data_fetch.php

<?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(); } } ?>

view_total_amount.php

<!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

Output

Year Month Total Amount
2016 August 150
2017 August 150
2017 September 300

Thank you !