Can CodeIgniter Helper Functions use database functions? Can CodeIgniter Helper Functions use database functions? codeigniter codeigniter

Can CodeIgniter Helper Functions use database functions?


You can get instance:

 $CI =& get_instance();

After that you will be able to use $CI->db for queries..


If you want to use $this in libraries, helpers, and access all the methods:

    $this->ci =& get_instance();    $this->ci->load->database();

You can do also:

    $this->ci->config->item('languages');

or

    $this->ci->load->library('session');


We can define a function in helper

if (!function_exists('getRecordOnId')){    function getRecordOnId($table, $where){        $CI =& get_instance();        $CI->db->from($table);        $CI->db->where($where);        $query = $CI->db->get();        return $query->row();    }}

and we can call from view like

$recordUser = getRecordOnId('users', ['id' => 5]); //here 5 is user Id which we can get from session or URL.