deviloper
deviloper

Reputation: 7240

customized query in codeigniter pagination class

I am using codeigniter pagination class and am stuck in this code! the following code reads the whole columns from the employees table but my query contains a join. I do not really know how I am going to run my own query using the config settings!

$config['per_page'] = 20; 
$view_data['view_data'] = $this->db->get('employees', $config['per_page'], $this->uri->segment(3));

My own query:

$this->db->select('emp.code as emp_code,emp.fname as emp_fname,emp.lname as emp_lname,emp.faname as emp_faname,emp.awcc_phone as emp_phone,emp.awcc_email as emp_email,position as position,dep.title as department');
$this->db->from('employees as emp');
$this->db->join('departments as dep','dep.code=emp.department_code');
$this->db->where('emp.status = 1');
$employees_list = $this->db->get();
return $employees_list;

Upvotes: 0

Views: 272

Answers (1)

Muhammad Raheel
Muhammad Raheel

Reputation: 19882

You approach is totally wrong. In Controller do it like this

$this->load->model('mymodel');
$config['per_page']     =   20; 
$view_data['view_data'] = $this->mymodel->getEmployees($config['per_page'], $this->uri->segment(3));

Modle function

function getEmployees($limit = 10 , $offset = 0)
{
    $select =   array(
                    'emp.code as emp_code',
                    'emp.fname as emp_fname',
                    'emp.lname as emp_lname',
                    'emp.faname as emp_faname',
                    'emp.awcc_phone as emp_phone',
                    'emp.awcc_email as emp_email',
                    'position as position',
                    'dep.title as department'
    );

    $this->db->select($select);
    $this->db->from('employees as emp');
    $this->db->join('departments as dep','dep.code=emp.department_code');
    $this->db->where('emp.status',1);
    $this->db->limit($offset , $limit);
    return $this->db->get()->result();
}

See this tutorial for usage

Upvotes: 0

Related Questions