How to call codeigniter controller function from view

后端 未结 14 1750
小蘑菇
小蘑菇 2020-11-27 05:53

How to call codeigniter controller function from view? When i call the function in a controller, get a 404 page.

14条回答
  •  轮回少年
    2020-11-27 06:55

    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;
            }  
        }
    }
    

提交回复
热议问题