Mohd Imran
Mohd Imran

Reputation: 90

retrieve logged in users events through codeigniter

Controller:

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

Model:

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

}

view:

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

I have two database table USERS and EVENTS. What i want is when user is logged in..all events of logged in user from EVENT table should display on users dashboard. right now it is displaying all events.

Upvotes: 0

Views: 1226

Answers (3)

ABorty
ABorty

Reputation: 2522

try something like this. When user try to login in your site then you need to do following things in your model.

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

now in your search_member_events function use session like

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

change all the input variable with yours.Please let me know if you face any problem.

Upvotes: 1

Akash KC
Akash KC

Reputation: 16310

Simply you can extract events of particular user from model in following way:

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

}

In controller action, you can get the userid from session and get the data accordingly:

$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); 

To set session, you have to first load 'session' library and then set the session with key and value.

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

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

change model function like:

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

}

controller:

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

may be you have some model function that does login after username and password check, in that same function you can set user id of loggedin user in session, by doing:

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

Upvotes: 1

Related Questions