[eluser]alijanah[/eluser]
Hi to All,
This is my first post actually. I just want to verify my coding methods to list data from db (in the model) and passing it to the view via the standard codeigniter parser helper. If anybody have any better ways, I would be more than grateful if you can share them with me. Thanks in advance.
Controller (test.php) - Basically a simple controller to call my model and assign it to the $query variable and them passing it to the view.
Code:
<?php
class Test extends Controller {
function __construct()
{
parent::Controller();
$this->load->model('test_model');
}
function index()
{
$query = $this->test_model->getCountry();
$data = array (
'title' => 'Country Listing',
'country_list' => $query
);
$this->parser->parse('test_view_parser', $data);
}
}
?>
My Model (test_model.php) - nothing spectacular here, just a simple call but returning the results with the result_array() function instead of result() function.
Code:
<?php
class Test_model extends Model {
function __construct()
{
parent::Model();
}
function getCountry()
{
$this->db->orderby('name asc');
$query = $this->db->get('country');
return $query->result_array();
}
}
?>
My View (test_view_parser.php) - self explanatory
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>{title}</title>
</head>
<body>
<h1>{title}</h1>
<ul>
{country_list}
<li><a href="{id}">{name}</a></li>
{/country_list}
</ul>
</body>
</html>
Is this the proper way of doing things or is there any other better ways ?. Thanks.