限制和偏移对Codeigniter3中的分页不起作用

时间:2018-05-28 17:58:04

标签: php codeigniter

我使用CI3并希望为我的网站使用分页。我使用本教程进行分页https://www.codexworld.com/ajax-pagination-in-codeigniter-framework/,它以某种方式工作。我在数据库中有12行并将限制设置为1,但是,它显示有12个页面(这是真的)但是从图像中获取数据库中的所有12行(查看滚动和页数):

Image

这是我的控制器:

function __construct() {
            parent::__construct();
            $this->load->library('Ajax_pagination');
            $this->perPage = 1;
        }


        public function index(){

            //total rows count
            $totalRec = count($this->collections_model->get_collections());
            $limit = $this->perPage;

            //pagination configuration
            $config['target']      = '#ajpag';
            $config['base_url']    = base_url().'collections/ajaxPaginationData';
            $config['total_rows']  = $totalRec;
            $config['per_page']    = $this->perPage;
            $this->ajax_pagination->initialize($config);

            //get the posts data

            $data['title'] = 'Collections';
            $data['collections'] = $this->collections_model->get_collections(FALSE, $limit);

            $this->load->view('templates/header', $data);
            $this->load->view('pages/index', $data);
            $this->load->view('templates/footer');
        }

        function ajaxPaginationData(){
            $page = $this->input->post('page');
            if(!$page){
                $offset = 0;
            }else{
                $offset = $page;
            }

            //total rows count
            $totalRec = count($this->collections_model->get_collections());
            $limit = $this->perPage;

            //pagination configuration
            $config['target']      = '#ajpag';
            $config['base_url']    = base_url().'collections/ajaxPaginationData';
            $config['total_rows']  = $totalRec;
            $config['per_page']    = $this->perPage;
            $this->ajax_pagination->initialize($config);

            //get the posts data
            $data['collections'] = $this->collections_model->get_collections(false, $limit, $offset);

            //load the view
            $this->load->view('collections/ajax-pagination-data', $data, false);
        }

这是我的模特:

public function get_collections($id = FALSE, $limit = FALSE, $offset = FALSE) {
        if ($id === FALSE) {
            $query = $this->db->get('collections');
            $this->db->order_by('id','desc');

            if($limit && $offset){
                $this->db->limit($limit,$offset);
            }elseif(!$offset && $limit){
                $this->db->limit($limit);
            }

            return $query->result_array();
        }

        $query = $this->db->get_where('collections', array('id' => $id));
        return $query->row_array();
    }

如何解决我的问题?

1 个答案:

答案 0 :(得分:0)

应用限制等后,您需要get

public function get_collections($id = FALSE, $limit = FALSE, $offset = FALSE) {
    if ($id === FALSE) {
        $this->db->order_by('id','desc');

        if($limit && $offset){
            $this->db->limit($limit,$offset);
        }elseif(!$offset && $limit){
            $this->db->limit($limit);
        }

        $query = $this->db->get('collections'); //call get here
        return $query->result_array();
    }

    $query = $this->db->get_where('collections', array('id' => $id));
    return $query->row_array();
}