dongweihuai5601 2015-03-27 09:26
浏览 38
已采纳

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

I just don't want to have such a duplicate function in a model, however I need to use that function again with a little change in it.

The little change is:

In the $first->get->model, I need to pass a segment url such as 'where id = $this->uri->segment(3)',

However, in the $second->get->model, I don't have that segment url anymore but have already save it to a session.

For example: (Please have a look to this code)

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

    }       

Is there a simple smart way to do this?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongya6395 2015-03-27 14:02
    关注

    You could either add a third argument and specify it every time or you could just test the segment in your function and if it is not there then use the session 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
    }
    

    So a full example would look something like:

    (I shortened up the above statement to a ternary expression for the example)

    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; 
            }
    
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序