在会话中保存uri段以将其发送回模型。笨

时间:2015-03-27 09:27:00

标签: php codeigniter

我只是不想在模型中有这样的重复功能,但是我需要再次使用该功能并稍作改动。

一点点变化是:

$first->get->model中,我需要传递一个分段网址,例如“where id = $this->uri->segment(3)”,

但是,在$second->get->model中,我没有该段网址,但已将其保存到会话中。

例如:(请查看此代码)

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; 
            }

    }       

有一种简单明智的方法吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

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

 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; 
        }

}