codeigniter分页不适用于应用的过滤器

时间:2014-09-18 09:21:54

标签: codeigniter pagination

我在codeigniter中遇到分页问题。当我应用过滤器来减少记录集时,分页似乎认为它仍在处理未过滤的数据。因此,例如,尽管返回的数据集中只有一页数据,但仍然显示了3个分页链接。点击第一个以外的任何一个将返回一个空白页。

这是我的控制器代码

function pro_client_list() {
    //called to open the List tab of the pro_clients_view
    $this->load->library('pagination');
    //this version is for localhost
    $config['base_url'] = '/index.php/main_controller/pro_client_list/';

    $config['total_rows'] = $this->pro_client_model->count_clients();;
    $config['per_page'] = 20;
    $config['full_tag_open'] = '<div class="pagination"><ul>';
    $config['full_tag_close'] = '</ul></div>';
    $this->pagination->initialize($config);
    $data = array();
    // DEV-140905A - check the state of the filters and only load matching clients
    $this->load->model('filter_model');
    $this->load->model('pro_client_model');
    $c_filter = $this->filter_model->get_client_category_filter();
    $y_filter = $this->filter_model->get_client_country_filter();
    $data['clients'] = $this->pro_client_model->get_clients($config['per_page'], $this->uri->segment(3), $c_filter, $y_filter);
    $this->load->model('filter_model');
    $data['categories'] = $this->filter_model->get_unique_categories();
    $data['countries'] = $this->filter_model->get_unique_countries();
    $this->load->view('includes/header');
    $this->load->view('pro_client_list', $data);
    $this->load->view('includes/footer');
}

这是模型的代码

function get_clients($num, $offset, $c_filter, $y_filter) {
    /*
    Returns a list of all clients in the database with pagination pointers
    Modified to return a count of the contacts belonging to each client 04/09/2014
    */
    $this->c_filter = $c_filter;
    $this->y_filter = $y_filter;
    $this->db->order_by("vch_name", "asc");
    $this->db->select('*, 
          (SELECT COUNT(*) FROM tbl_contact WHERE fk_client_id=tbl_pro_client_id) AS count_contacts,
          (SELECT MIN(vch_next_interact) FROM tbl_contact WHERE fk_client_id=tbl_pro_client_id) AS next_interact'
        , false);

    if($c_filter != '-1') {
        $this->db->where('vch_category', $this->c_filter);
    }
    if($y_filter != '-1') {
        $this->db->where('vch_country', $this->y_filter);
    }
    $query = $this->db->get('tbl_pro_client', $num, $offset);
    return $query;
}

请注意,在添加“类别”和“国家/地区”过滤器之前,分页工作正常。

非常感谢任何帮助,

Ú

1 个答案:

答案 0 :(得分:0)

好吧,我担心这只会归结为脑部疾病。在设置分页$ config时我有一行

$config['total_rows'] = $this->pro_client_model->count_clients();

当然,如果我应用过滤器,那么我需要重新计算行数,所以我修改了函数以使用与显示函数相同的过滤器。它现在看起来像这样:

$config['total_rows'] = $this->pro_client_model->count_clients($c_filter, $y_filter);

努力享受!

我真的不应该在咖啡因开始之前的早晨尝试编码: - )

感谢您查看此内容。

Ú