Skip to content Skip to sidebar Skip to footer

CodeIgniter fetch data to the view without controller

Reading Time: < 1 minute

How to direct call to the function from the view without using controller in CodeIgniter.Here is a example to it.Simply call to helper function and it get data from model.
First you load helper file from your controller.

A helper class is a set of functions. These helper functions can help a library or any other type of function that you want to use over and over again in your code.

In the controller

$this->load->helper('user_helper');

user_helper.php

function getUserDetails($userId=NULL,$field='')
{
   $CI =& get_instance();
   $mod = $CI->load->model('user_model');
   $conditions = array('users.id'=>$userId);
   $result = $CI->user_model->getUsers($conditions);
   if($result->num_rows()>0) {
        $data = $result->row();
        $res = $data->$field;
   } else {
        $res = '';
   }
return $res;
}

Usage in your view

 getUserDetails($user_id,'user_name')

That’s all!