In our last tutorial we have learned storing data in MySql database using CodeIgniter Active Record Class. Today we will generate a report. First off we will create a blank template page, called “report.php” and then a “controllers” page called “report.php” with the following code.
class Report extends CI_Controller
{
function __construct()
{
parent:: __construct();
$this->load->helper(array('form', 'url', 'html'));
}
}
Thirdly, we will create a “Models” page called “report_model.php” with the code below
class report_model extends CI_Model
{
function __construct()
{
parent:: __construct();
}
}
The update your “report_model.php” with the following code
class report_model extends CI_Model {
function __construct()
{
parent:: __construct();
}
function select_data()
{
$query = $this->db->get('report');
return $query->result();
}
}
Update your “Controllers” page with following code
class Report extends CI_Controller
{
function __construct()
{
parent:: __construct();
$this->load->helper(array('form', 'url', 'html'));
}
public function report_gen($page = 'report')
{
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
show_404();
}
$data['title'] = ucfirst($page);
$data['query'] = $this->report_model->select_data();
$this->load->view('templates/header', $data);
$this->load->view('pages/report',$data);
$this->load->view('templates/footer', $data);
}
}
Finally update your template page with this code
<table width="642" cellspacing="0" cellpadding="0">
<th>
<td>SI No.</td>
<td>Full Name</td>
<td>Email</td>
</th>
<?php foreach($query as $row){ ?>
<tr>
<td><?=$row->id;?></td>
<td><?=$row->full_name;?></td>
<td><?=$row->email;?></td>
</tr>
<?php } ?>
</table>
Now visit – http://localhost/your_root_folder/index.php/controllers_name/controllers_function_name
to see the report.
Example : http://localhost/CodeIgniter/index.php/ report/ report_gen
