Can CodeIgniter Helper Functions use database functions?

前端 未结 4 991
不思量自难忘°
不思量自难忘° 2021-02-05 13:37

One of my CodeIgniter Controller functions needs to call a recursive function as part of its functionality. The function call chokes if I put it inside the controller class, an

4条回答
  •  半阙折子戏
    2021-02-05 14:25

    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.
    

提交回复
热议问题