【问题标题】:customized query in codeigniter pagination classcodeigniter 分页类中的自定义查询
【发布时间】:2013-04-21 05:45:27
【问题描述】:

我正在使用 codeigniter 分页类并且卡在这段代码中! 以下代码从 employees 表中读取整个列,但我的查询包含一个联接。我真的不知道如何使用配置设置运行我自己的查询!

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

我自己的查询:

$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;

【问题讨论】:

    标签: codeigniter pagination


    【解决方案1】:

    你的做法是完全错误的。 在Controller中这样做

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

    模型功能

    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();
    }
    

    使用方法见this教程

    【讨论】:

      猜你喜欢
      • 2016-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-25
      • 1970-01-01
      • 2010-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多