codeigniter分页类中的自定义查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codeigniter分页类中的自定义查询相关的知识,希望对你有一定的参考价值。

我正在使用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;
答案

你接近是完全错误的。在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教程

以上是关于codeigniter分页类中的自定义查询的主要内容,如果未能解决你的问题,请参考以下文章

如何使 Codeigniter 中的分页类与 AJAX 一起工作?

Codeigniter分页类,其中包含导航中的项目符号和图像

过滤类 分页类源码 APIView实现分页 JWT认证

始终使用 CodeIgniter 分页类显示上一个和下一个链接

python---django中自带分页类使用

php 分页 分页类 简单实用