How to call codeigniter controller function from view

后端 未结 14 1661
小蘑菇
小蘑菇 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;
            }  
        }
    }
    
    0 讨论(0)
  • 2020-11-27 06:58

    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. :)

    0 讨论(0)
提交回复
热议问题