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

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.

class Test extends Controller {
    function __construct()
    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.

class Test_model extends Model {

    function __construct()
    function getCountry()
        $this->db->orderby('name asc');
        $query = $this->db->get('country');
        return $query->result_array();



My View (test_view_parser.php) - self explanatory
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

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



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


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

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.

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