totalnewb
totalnewb

Reputation: 17

Retrieve data based on user id (CodeIgniter)

Im fairly new with php so please bear with me. I have two tables in my database, users and onlineform tables. The onlineform table have userid field as a foreign key for the id field in users table. Im trying to display data from onlineform table based on current user login. The problem is, it display data from all users and not the one who logged in. I have setup a query in my form_model but i dont know if the query is correct

this is the model

form_model.php:

function getEmployees() 
{
    $this->db->select("users.id, onlineform.date, onlineform.day, onlineform.in1, 
        onlineform.out1, onlineform.in2, onlineform.out2");
    $this->db->from('users');
    $this->db->join('onlineform','users.id = onlineform.userid', 'left');
    $this->db->order_by('date asc');
    $query = $this->db->get();
    return $query->result();
}

this is the controller

form.php

public function table()
{
    if($this->session->userdata('username') != '')  
       {     
            $this->load->model('form_model');
            $query = $this->form_model->getEmployees();
            $data['EMPLOYEES'] = null;
            if($query) {
                $data['EMPLOYEES'] =  $query;
            }
       } 

    else  
       {  
            redirect(base_url() . 'index.php/form/login');  
       }

            $this->load->view('table.php', $data);
}

and this is my view

table.php

<?php 
        foreach($EMPLOYEES as $employee)
          {?>
         <tr>
          <td align="center" 
          <?php if($employee->day === "SAT" || $employee->day === "SUN"): ?> style="background-color:#A9A9A9; color: white;" <?php endif; ?>>
            <strong><?=$employee->day;?></strong>
          </td>
          <td align="center" 
          <?php if($employee->day === "SAT" || $employee->day === "SUN"): ?> style="background-color:#A9A9A9; color: white;" <?php endif; ?>>
            <strong><?=$employee->date;?></strong>
          </td>
          <td><?=$employee->out1;?></td>
          <td><?=$employee->in1;?></td>
          <td><?=$employee->out2;?></td>
          <td><?=$employee->in2;?></td>
         </tr>     
        <?php }?>

Upvotes: 1

Views: 2460

Answers (2)

Ropali Munshi
Ropali Munshi

Reputation: 3006

You can save the current logged in userid in session then you can use it when you retrieve the data from database. change your form_model.php code to this

function getEmployees() 
{
    $this->db->select("users.id, onlineform.date, onlineform.day, onlineform.in1, 
        onlineform.out1, onlineform.in2, onlineform.out2");
    $this->db->from('users');
    $this->db->join('onlineform','users.id = onlineform.userid', 'left');
    $this->db->where("users.id", $this->session->userdata('current_user_id'));
    $this->db->order_by('date asc');
    $query = $this->db->get();
    return $query->result();
}

Upvotes: 0

Hilal Arsa
Hilal Arsa

Reputation: 171

You can pass the current logged in userId into the model through getEmployees($userid)

$query = $this->form_model->getEmployees($userid); 

And then use it in your query to get current userId that matches logged in user, to user in database;

function getEmployees($userid) 
{
    $this->db->select("users.id, onlineform.date, onlineform.day, onlineform.in1, 
    onlineform.out1, onlineform.in2, onlineform.out2");
    $this->db->from('users');
    $this->db->where('onlineform','users.id',$userid); //add this line
    $this->db->join('onlineform','users.id = onlineform.userid', 'left');
    $this->db->order_by('date asc');
    $query = $this->db->get();
    return $query->result();
}

And then pass it normally to your view.

Hope this helps.

Upvotes: 1

Related Questions