codeigniter分页页码问题

时间:2015-02-25 17:44:31

标签: php database codeigniter pagination

我试图使用Codeigniter分页库,我希望我的分页网址如下:

mysite.com/codeigniter/controller/page/1

mysite.com/codeigniter/controller/page/2

mysite.com/codeigniter/controller/page/3

这是我的控制器:

class Controller extends CI_Controller {
    public function index() {
        $this->load->model('model');
        $segment=$this->uri->segment(3);
        /* Pagination */
        $config['base_url']=base_url('controller/index');
        $config['total_rows']=$this->model->count_active_members()->num_rows;
        $config['per_page']=5;
        $config['uri_segment'] = 3;
        $config['use_page_numbers'] = TRUE;
        $this->pagination->initialize($config);
        /* END Pagination */
        $result['members']=$this->model->get_active_members($segment);
        $this->load->view('view',$result);
    }
}

我的模特:

class Model extends CI_Model {
    public function get_active_members($segment) {
        $this->db->select('username,email,balance,ctimes,regdate');
        $this->db->limit(5,$segment);
        return $this->db->get_where('members',array('status'=>'active'));
    }

    public function count_active_members() {
        return $this->db->get_where('members',array('status'=>'active'));
    }   
}

它按照我想要的方式工作,但问题是我在我的db&中有9行它只显示7

1 个答案:

答案 0 :(得分:2)

我认为你的问题是偏移,试试这个:

$this->db->select('username,email,balance,ctimes,regdate');
$per_page = 5;
$segment = $segment > 0 ? (($segment - 1) * $per_page) : $segment;
$this->db->limit($per_page, $segment);