Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 向Codeigniter中的库发送函数_Php_Codeigniter - Fatal编程技术网

Php 向Codeigniter中的库发送函数

Php 向Codeigniter中的库发送函数,php,codeigniter,Php,Codeigniter,这在我的控制器中工作 $this->load->driver('cache'); //working // $data['advisory']=$advisory = $this->cache->memcached->get('advisory'); // if(! $advisory) // { // $data['advisory']=$advisory = $this->Mhomework->getbyadvisory($this->

这在我的控制器中工作

$this->load->driver('cache');

//working
// $data['advisory']=$advisory = $this->cache->memcached->get('advisory');
// if(! $advisory)
// {
//     $data['advisory']=$advisory = $this->Mhomework->getbyadvisory($this->teacherid);
//     $this->cache->memcached->save('advisory' , $advisory);
// }
我制作了一个图书馆,如下所示,以便我可以为其他人使用它

function cache($key, $data) 
{
    $this->CI->load->driver('cache');
    $cache = $this->CI->cache->memcached->get($key);

    if (!$cache) {
        // There's been a miss, so run our data function and store it
        $cache = $data($CI);
        //$cache = $data;
        $this->CI->cache->memcached->save($key, $cache);
    }

    return $cache;
}
在控制器中,我改成了下面的命令,这给出了一个错误

$data['advisory'] = $this->hwtracker->cache('advisory.'.$this->teacherid, function(&$CI){
        return $CI->Mhomework->getbyadvisory($this->teacherid);
    });

// error  Call to a member function getbyadvisory() on a non-object in ... controller
// Message: Trying to get property of non-object
我的问题是如何将函数发送到CodeIgniter中的库?或者我可以吗

更新:    

function getbyadvisory($id){
          $Q="SELECT *, studentid, COUNT(studentid),be_user_profiles.first_name,   
       be_user_profiles.last_name
            FROM be_user_profiles
            LEFT JOIN hw_homework
            ON be_user_profiles.user_id= hw_homework.studentid
            WHERE be_user_profiles.advisor = $id
            GROUP BY be_user_profiles.user_id
            ORDER BY COUNT(studentid) DESC";
        $query = $this->db->query($Q);

        if ($query->num_rows() > 0)
        {
            foreach ($query->result_array() as $row)
            {
                $data[] = $row;
            }

        }
        else
        {
            $data = FALSE;
        }
        $query->free_result();
        return $data;
    }

请将您的库更改为这样

function cache($key, $data) 
{
 $this->CI->load->driver('cache');

 //bind your key here
 $key_details = $data.$key;

 $cache = $this->CI->cache->memcached->get($key_details);

 if (!$cache) {

    // There's been a miss, so run our data function and store it
    $cache = $this->$data($key);

    //$cache = $data;
    $this->CI->cache->memcached->save($key, $cache);
 }

   return $cache;
}

//new advisory function in library
function advisory($teacherid){

    return $this->CI->Mhomework->getbyadvisory($teacherid);
}
$data['advisory'] = $this->hwtracker->cache($this->teacherid,'advisory');
在控制器中,像这样调用

function cache($key, $data) 
{
 $this->CI->load->driver('cache');

 //bind your key here
 $key_details = $data.$key;

 $cache = $this->CI->cache->memcached->get($key_details);

 if (!$cache) {

    // There's been a miss, so run our data function and store it
    $cache = $this->$data($key);

    //$cache = $data;
    $this->CI->cache->memcached->save($key, $cache);
 }

   return $cache;
}

//new advisory function in library
function advisory($teacherid){

    return $this->CI->Mhomework->getbyadvisory($teacherid);
}
$data['advisory'] = $this->hwtracker->cache($this->teacherid,'advisory');

发布
getbyadvisory()
函数的代码,我认为您正在传递
object
,同时它接受数组。请确保
Mhomework
已加载。$this->CI->load->model('welcome/Mhomework');在库构造函数中加载。1。我想在$this->hwtracker->cache()中添加一个函数,而不是在库中。2.它在第278行的/Users/teacher/Sites/hw_current2/application/libraries/Hwtracker.php中对未定义的方法Hwtracker::data()进行错误调用