Ror*_*rik 13 php recursion controller codeigniter helper
我的一个CodeIgniter Controller函数需要调用递归函数作为其功能的一部分.如果我将它放在控制器类中,函数调用会产生阻塞,如果我把它放在类之外,它就无法访问数据库函数($this->db->get()).将其作为辅助功能可以解决这个问题吗?
Sve*_*lav 34
你可以得到实例:
$CI =& get_instance();
Run Code Online (Sandbox Code Playgroud)
之后,您将能够$CI->db用于查询..
如果你想在库,帮助器中使用$ this,并访问所有方法:
$this->ci =& get_instance();
$this->ci->load->database();
Run Code Online (Sandbox Code Playgroud)
你也可以这样做:
$this->ci->config->item('languages');
Run Code Online (Sandbox Code Playgroud)
要么
$this->ci->load->library('session');
Run Code Online (Sandbox Code Playgroud)
我们可以在 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();
}
}
Run Code Online (Sandbox Code Playgroud)
我们可以从视图中调用
$recordUser = getRecordOnId('users', ['id' => 5]); //here 5 is user Id which we can get from session or URL.
Run Code Online (Sandbox Code Playgroud)