Christeen Christeen - 1 month ago 15
PHP Question

codeigniter where_not_in not working

I pass an array to my model and I want to get the employee_id s which are not in that array. Also 'position' field should be equal to 'c'.
Following is my table.
emp_position table

Following is my model. $evdata is a number passed to the model to limit the selecting records. Also the array $data is json encoded array.

public function get_camaramens($data,$evdata)
{

$this->db->select('emp_position.employee_id');
$this->db->from('emp_position');
$this->db->where('emp_position.position','c');
$this->db->where_not_in('emp_position.employee_id',$data);
$query=$this->db->get('',$evdata);
return $query->result();


}

It doesn't give a correct result. So I removed the following part and checked.

$this->db->where_not_in('emp_position.employee_id',$data);


Both times it gives the same result. It seems only the first where condition is working.

[Following is the var_dump result of both times.][2]


After adding the code

$data = $this->emp_event_model->get_current_employees($date);

$newData = array();
foreach($data as $row) {
$newData[] = $row['employee_id'];
}
$data = $newData;


It gives this error
enter image description here

Answer

your $data array must be in this format

array(2, 3, 7, 8, 9)

but your array is

array (size=5) 
0 => object(stdClass)[26] public 'employee_id' => string '2' (length=1) 
1 => object(stdClass)[27] public 'employee_id' => string '3' (length=1) 
2 => object(stdClass)[28] public 'employee_id' => string '7' (length=1) 
3 => object(stdClass)[29] public 'employee_id' => string '8' (length=1) 
4 => object(stdClass)[30] public 'employee_id' => string '9' (length=1) 

so restructure your array inside your controller

$newData = array();
foreach($data as $row) {
    $newData[] = $row->employee_id;
}
$data = $newData;

so this will become

array(2, 3, 7, 8, 9)