通过codeigniter检索登录的用户事件

时间:2013-09-24 05:28:13

标签: php codeigniter

控制器:

public function searchevent() {
    $this->load->model("users_model");  
    $data["rows"] = $this->users_model->search_member_events();
    $this->load->view("search_view", $data);        
}

型号:

public function search_member_events() {
    $this->db->select('*');
    $this->db->from('users');
    $this->db->where('id', '$userid');
    $this->db->join('events', 'users.id = events.user_id');
    $q = $this->db->get();      
    if ( $q->num_rows() > 0 ) {
        foreach ( $q->result() as $row ) {
            $data[] = $row;
        }
        return $data;
    }

}

视图:

<div class="member_search_results">
   <h2>Your Events </h2>
   <?php if(isset($rows)) : foreach($rows as $r): ?>
      <div class="outer">
        <div class='single_result'>
           <strong class='title'><?php echo $r->title ?></strong>
           <div class="outer">
               <span class='start'><strong>Starts at:</strong> <?php echo $r->start ?></span>
               <span class='end'><strong>Ends at:</strong> <?php echo $r->end ?></span>
               <span class='time'><strong>Time:</strong><?php echo $r->time ?></span>               
           </div>
           <div class="outer">
               <span class='location'><strong>Location:</strong><?php echo $r->location ?></span>
           </div>
           <div class="outer">
               <span class='description'><strong>Details:</strong><?php echo $r->description ?></span>
           </div>
        </div>
        <?php endforeach; ?>
    <?php else : ?>
    <h2>You do no have any events yet, please <?php echo anchor ("createevent", "Create Event") ?></h2>
    <?php endif; ?>
</div>

我有两个数据库表USERS和EVENTS。我想要的是当用户登录时。来自EVENT表的所有已登录用户的事件应显示在用户仪表板上。现在它正在显示所有事件。

3 个答案:

答案 0 :(得分:1)

更改模型功能,如:

public function search_member_events($user_id){     

    $this->db->select('*');
    $this->db->from('users');
    $this->db->join('events', 'users.id = events.user_id');
    $this->db->where('users.id',$userid);    
    $q = $this->db->get();      
    if($q->num_rows() > 0){
        foreach($q->result() as $row){
            $data[] =   $row;
        }
        return $data;
    }

}

控制器:

public function searchevent(){
    //get from session if you've set userid on login, like
    $user_id = $this->session->userdata("user_id");
    $this->load->model("users_model");  
    $data["rows"] = $this->users_model->search_member_events($user_id);
    $this->load->view("search_view", $data);        
}

可能是你有一些模型功能在用户名和密码检查后登录,在同一个功能中你可以设置登录用户在会话中的用户ID,通过这样做:

//get the user id of loggedin user after 
//login check and set in session
$this->session->set_userdata("user_id", $user_id);

答案 1 :(得分:1)

您可以通过以下方式从模型中提取特定用户的事件:

public function search_member_events($userid){     

$this->db->select('*');
$this->db->from('users');
$this->db->join('events', 'users.id = events.user_id');
$this->db->where('id',$userid);
$q = $this->db->get();      
if($q->num_rows() > 0){
    foreach($q->result() as $row){
        $data[] =   $row;
    }
    return $data;
}

}

在控制器操作中,您可以从会话中获取用户ID并相应地获取数据:

$this->load->model("users_model");  
$userid = $this->session->userdata('userid');// Assumed that you've already set userid in session
$data["rows"] = $this->users_model->search_member_events($userid);
$this->load->view("search_view", $data); 

要设置会话,您必须首先加载“会话”库,然后使用键和值设置会话。

$this->load->library('session');
$this->session->set_userdata('key', 'value'); 

答案 2 :(得分:1)

尝试这样的事情。当用户尝试登录您的网站时,您需要在模型中执行以下操作。

$sess_arr=array();
$pass   = $this->input->post('password',true); //change password field with yours
$email  = $this->input->post('emailLogin',true); //change emailLogin field with yours

$rs=$this->db->select('id')->where('email',$email)->where('password',$pass)->get('users');
if($rs->num_rows()>0)
{
    $data=$rs->row_array();
    $sess_arr['id'] =$data['id'];
    $this->session->set_userdata('user_login',$sess_arr); //it will set the logged in user id to user_login variable
}

现在在您的search_member_events函数中使用会话

$user_id=$this->session->userdata['user_login']['id'];

用你的更改所有输入变量。如果你遇到任何问题,请告诉我。