CodeIgniter Basic – Calling model or database from views

Okay, this tips might be not the best practice to do because model should be called from controllers and database should be called from models (as per MVC guidelines). But, if you are no web-designer (like me) and in a hurry to test something inside your view files that involve database queries, this tips should suffice your need.

You can’t simply call models (or database) from your view files like this:


Instead, you should do like these examples:

$CI =& get_instance();

//calling database
foreach ($q_kr->result_array() as $row) {
    //do something here, maybe creating a list

//loading a model
//do something with the model

I hope by examining those example, everything is clear.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>