How to call codeigniter controller function from view? When i call the function in a controller, get a 404 page.
I had this same issue , but after a couple of research I fond it out it's quite simple to do,
Locate this URL in your Codeigniter project: application/helpers/util_helper.php
add this below code
//you can define any kind of function but I have queried database in my case
//check if the function exist
if (!function_exists('yourfunctionname')) {
function yourfunctionname($param (if neccesary)) {
//get the instance
$ci = & get_instance();
// write your query with the instance class
$data = $ci->db->select('*');
$data = $ci->db->from('table');
$data = $ci->db->where('something', 'something');
//you can return anythting
$data = $ci->db->get()->num_rows();
if ($data > 0) {
return $data;
} else {
return 0;
}
}
}
I know this is bad.. But I have been in hard situation where it is impossible to put this back to controller or model.
My solution is to call a function on model. It can be do inside a view. But you have to make sure the model has been loaded to your controller first.
Say your model main_model, you can call function on the model like this on your view : $this->main_model->your_function();
Hope this help. :)