Calling Model from View (Codeigniter) Calling Model from View (Codeigniter) codeigniter codeigniter

Calling Model from View (Codeigniter)


You can use this:

   $ci =&get_instance();   $ci->load->model(your model);   $ci->(your model)->(your function);        Note: You have to call your model in your controller.Its working fine


You can simply the call the model as you call in the controller like this

$this->load->model('Model_name');$this->Model_name->function(); 

Or in case you dont want to call directly in the view you can get the data through the controller:

$categoryID =  $this->Model_name->function();$categoryDetails =  $this->Model_name->function($categoryID);$data = [    'categoryID'      => $categoryID,    'categoryDetails' => $categoryDetails];

And then

$this->load->view('view name',$data);


Try this as your model:

function get_posts(){             $this->db->get('post');             $this->db->join('category', 'posts.category_id = category.id');    $query = $this->db->get();    return $query->result(); }

Do check if the table and column names in the join section are correct. 'category' is the name of your category table, posts.category_id is the column in your posts table that refers to the category id, category.id is the id column in the category table.

You should be able to access the category name now.