Home > Software engineering >  How to load user data using codeigniter
How to load user data using codeigniter

Time:07-07

When I use different user account, it still load the same data from database. I want to display only the data of the specific user. How can I load the data of specific user? Thank you for your response.

 Here is my view using ajax query. 
 //loading the content
 $(document).ready(function(){
 {
 $.ajax({
  url:"<?php echo base_url(); ?>kpi/loaddatatest",
  dataType:"JSON",
  method:"post",
  success:function(data){
 //Table will create when the ## ##     
    var html = '<tr>';  
    html  = '<td id="kpi_description" contenteditable placeholder="Enter your KPI"></td>';
    html  = '<td id="kra_description" contenteditable placeholder="Enter your KRA"></td>';
    html  = '<td id="kpi_weight" contenteditable></td>';
    html  = '<td></td>';
    html  = '<td></td>';
    html  = '<td><button type="button" name="btn_add" id="btn_add" ><span > </span></button></td></tr>';
    for(var count = 0; count < data.length; count  )    
    {
    
      html  = '<tr>';
      html  = '<td  data-row_id="' data[count].id '" data- 
       column_name="kpi_description" contenteditable>' data[count].kpi_description '</td>';
      html  = '<td  data-row_id="' data[count].id '" data- 
          column_name="kra_description" contenteditable>' data[count].kra_description '</td>';
      html  = '<td  data-row_id="' data[count].id '" data- 
       column_name="kpi_weight" disabled>' data[count].kpi_weight '</td>';
      html  = '<td></td>';
      html  = '<td></td>';
      html  = '<td><button type="button" name="delete_btn" id="' data[count].id '" ><span >x</span></button> 
      </td></tr>';
    }
    $('#testtable').html(html);
    
   }
  });
 }

Here is my controller: public function loaddatatest() {

    $data = $this->Kpi_model->loaddatatest();
    
    echo json_encode($data);
}

Here is my model: public function loaddatatest() {

    $this->db->order_by('id', 'ASCE');
    $data = $this->db->get('kpi_result'); 


    if($data->num_rows()>0)
        {

            return $data->result();
        }
        else
        {
            return false;
        }
}

CodePudding user response:

To query specific user make sure your database has id_user, when query can be done like this in loaddatatest()function using get_where().

$this->db->get_where('user_id', array('user_id => $user_id))

And you can pass specific user id through loaddatatest() argument like this

$data = $this->Kpi_model->loaddatatest($user_id);

CodePudding user response:

I have employee_id in my database. and my table name in the database is 
kpi_result. controller: 

 public function loaddatatest() {
  $data = $this->Kpi_model->loaddatatest($user_id); 
  echo json_encode($data); } 
model:
   public function loaddatatest() {
   $this->db->order_by('id', 'ASC'); $data = $this->db->get_where('kpi_result', 
   ['employee_id' => $user_id])->result_array();
} 
I tried these code but not working. No data display
  • Related