My model method always gives "No User Found". Why is it that my query never returns any rows?
When $page is 0 and $offset is 5, I expect to see the first 5 rows from the database table
Controller
public function filter($page = 0, $offset = 5, $search = '')
{
$search = $this->input->post('search');
$row = $this->m_user->getAllFilterUsers($offset, $page, $search);
$data['usersTable'] = $row->result();
// more scripting
}
Model
public function getAllFilterUsers($offset, $count, $search)
{
if ($search != '') {
$this->db->where('user_position', 'client');
$this->db->where('user_status', $search);
}
$this->db->where('user_position','client');
$this->db->where('user_status','active');
$this->db->order_by('user_id', 'desc');
$UsersQuery = $this->db->get('tb_user', $offset, $count);
if($UsersQuery->num_rows>0){
return $UsersQuery;
} else {
$this->session->set_flashdata('message','No User Found');
redirect('cuser/filter','refresh');
}
}
Just check getAllFilterUsers function of your model. The value of $offset is 5 and value of $count is 0. But you use..
this->db->where('user_position','client');
$this->db->where('user_status','active');
$this->db->order_by('user_id', 'desc');
$UsersQuery = $this->db->get('tb_user',$offset,$count);
that means..
Select * from tb_user where user_position = 'client' and 'user_status' = 'active' order by user_id desc limit 5,0
This query always return empty set Your query should be..
Select * from tb_user where user_position = 'client' and 'user_status' = 'active' order by user_id desc limit 0,5
So, Your code snippet should be
this->db->where('user_position','client');
$this->db->where('user_status','active');
$this->db->order_by('user_id', 'desc');
$UsersQuery = $this->db->get('tb_user',$count,$offset);
Finally the function is...
public function getAllFilterUsers($offset,$count,$search){
if($search!=''){
$this->db->where('user_position','client');
$this->db->where('user_status',$search);
}
this->db->where('user_position','client');
$this->db->where('user_status','active');
$this->db->order_by('user_id', 'desc');
$UsersQuery = $this->db->get('tb_user',$count,$offset);
if($UsersQuery->num_rows>0){
return $UsersQuery;
}
else{
$this->session->set_flashdata('message','No User Found');
redirect('cuser/filter','refresh');
}
}
Thats it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With