在会话中保存uri段以将其发送回模型.代码点火器


save uri segment in session to send it back to the model. Codeigniter

我只是不想在模型中有这样一个重复的函数,但我需要再次使用该函数,并对其进行一些更改。

变化不大的是:

$first->get->model中,我需要传递一个段url,如"where id = $this->uri->segment(3)"、

然而,在$second->get->model中,我不再有那个分段url,但已经将其保存到会话中。

例如:(请查看此代码)

function get_regidsterLogin($limit, $offset) //There is no problem with $limit and $offset
    {
        $this->load->database();
        $this->db->select(' ds.userid,
                            ds.register_time,
                            gl.logintime
                          '); 
        $this->db->from('data_stats ds');
        $this->db->join('game_login gl', 'gl.data_id = ds.dataid', 'inner');
        //Here is what I want to change with session that I will send from view or maybe controller
        $this->db->where('DATE(register_time)', $this->uri->segment(3));
        $this->db->limit($limit, $offset);
        $query = $this->db->get(); 
          if($query->num_rows() > 0)
            {
                foreach ($query->result() as $row)
                {
                    $data[] = $row;
                }
                    return $data; 
            }
    }       

有没有一种简单而聪明的方法可以做到这一点?

提前谢谢。

您可以添加第三个参数并每次指定它,也可以只测试函数中的段,如果不存在,则使用会话数据:

 if($this->uri->segment(3) === TRUE{
     $uri = $this->uri->segment(3);
} else {
     $uri = $this->session->userdata('user_id') // or whatever your session variable is
}

因此,一个完整的例子看起来像:

(对于这个例子,我把上面的语句缩短为三元表达式)

function get_regidsterLogin($limit, $offset) //There is no problem with $limit and $offset
{
    $uri = ($this->uri-sement(3) == true ? $this->uri-sement(3) : $this->session->userdata(id));
    $this->load->database();
    $this->db->select(' ds.userid,
                        ds.register_time,
                        gl.logintime
                      '); 
    $this->db->from('data_stats ds');
    $this->db->join('game_login gl', 'gl.data_id = ds.dataid', 'inner');
    //Here is what I want to change with session that I will send from view or maybe controller
    $this->db->where('DATE(register_time)', $uri);
    $this->db->limit($limit, $offset);
    $query = $this->db->get(); 
      if($query->num_rows() > 0)
        {
            foreach ($query->result() as $row)
            {
                $data[] = $row;
            }
                return $data; 
        }
}