Lev
Lev

Reputation: 65

Codeigniter - Get_where base from session_userdata

I want to select all the rows that emp id's are match to my session userdata (emp_id). Here's my code. I get so many errors and no row has been selected. Someone help me please. Thanks

Model:

public function get_save_samp($emp_id) {
   $query = $this->db->get_where('tblsavesample', array('emp_id' => 
       $emp_id));
   return $query->row_array();
}

Controller:

public function show() {

    $emp_id = $this->session->userdata('emp_id');
    $data['save'] = $this->user_model->get_save_samp($emp_id);
    $this->load->view('show',$data);
}

Views:

<?php foreach ($save as $row) { ?>                                                                                      
    <td style="width: " ><?php echo $row->emp_id ?></td>
    <td style="width: " ><?php echo $row->emp_code?></td>
    <td style="width: " ><?php echo $row->emp_name ?></td>
<?php   }>

Upvotes: 0

Views: 647

Answers (5)

geeth
geeth

Reputation: 714

row_array() used to return only single record. change your code in model as follows :

public function get_save_samp($emp_id) {  

$query = $this->db->get_where('tblsavesample', array('emp_id' => 
   $emp_id));

return $query->result_array();

}

and check number of records returned in your controller as follows:

$data['save'] = $this->user_model->get_save_samp($emp_id);
echo $data['save']->num_rows();

if this displays 0, then you have no matching records in your database table.

to test the query, try to execute the same query in mysql

Upvotes: 0

Ketan Solanki
Ketan Solanki

Reputation: 697

Change your Modal Code like this :

  public function get_save_samp($emp_id) {
     //$query = $this->db->get_where('tblsavesample', array('emp_id' =>$emp_id));
     $this->db->select("*"); 
     $this->db->where_in('id', $emp_id);
     $query = $this->db->get('tblsavesample');
     return $query->result();
  }

And Change your Controller code like this to check what data are we getting inside controller.

public function show()
{    
    $emp_id       = $this->session->userdata('emp_id');
    $data['save'] = $this->user_model->get_save_samp($emp_id);
    echo "<pre>";
    print_R($data['save']);
    exit();
    $this->load->view('show', $data);
}

Upvotes: 0

Lomesh Kelwadkar
Lomesh Kelwadkar

Reputation: 110

row_array() returns the first row only. If you want all the records to be returned, use result_array() instead.

$result = $query->result_array();
return $result;

Link : click

Upvotes: 0

NikuNj Rathod
NikuNj Rathod

Reputation: 1658

you can try solution for your problem :

Changes your modal function :

Modal.php

public function get_save_samp($emp_id) {
     $this->db->select("*"); 
     $this->db->where('id', $emp_id);
     $this->db->get('tblsavesample');
     return $query->row();
}

Views:

<?php foreach ($save as $row) {?>
   <td style="width: " ><?php echo $row->emp_id ?></td>
   <td style="width: " ><?php echo $row->emp_code ?></td>
   <td style="width: " ><?php echo $row->emp_name ?></td>
<?php } ?>

Upvotes: 2

Vijay Sharma
Vijay Sharma

Reputation: 831

Try This

public function get_save_samp($emp_id) {
       $query = $this->db->get_where('tblsavesample', array('emp_id' => 
       $emp_id));
        return $query->result();
       }

Upvotes: 2

Related Questions