Welcome Guest, Not a member yet? Register   Sign In
Using Parser helper to list data from db
#1

[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">

&lt;html &gt;
&lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"/&gt;
    &lt;title&gt;{title}&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

<h1>{title}</h1>

<ul>
{country_list}
    <li><a href="{id}">{name}</a></li>
{/country_list}
</ul>

&lt;/body&gt;
&lt;/html&gt;

Is this the proper way of doing things or is there any other better ways ?. Thanks.
#2

[eluser]sophistry[/eluser]
Welcome to CI... and very nice first post.

This looks ideal - very clean code - and exactly the way CI is set up to help you organize.

The only suggestion I have (and I had to look hard for something to criticize) is to pick a good variable naming style: you should rename $query in your controller to $result_array or something like that so that it helps you remember that your model returns an array rather than the actual CI query object. Very minor point, but it will help you keep all your "ducks in a row" as you expand your code.
#3

[eluser]alijanah[/eluser]
Thanks for your comment sophistry. Yes I agree, that's an oversight on my part not renaming the $query return value to something like $country_list_array. So it looks like I'm on a right track here as far as using the template parser is concerned.




Theme © iAndrew 2016 - Forum software by © MyBB