在CodeIgniter中實(shí)現(xiàn)分頁功能是非常簡單的,可以按照以下步驟進(jìn)行操作:
application/config
目錄下的autoload.php
文件中加載database
庫和pagination
庫:$autoload['libraries'] = array('database','pagination');
pagination
庫,并設(shè)置分頁配置參數(shù):$this->load->library('pagination');
$config['base_url'] = 'http://example.com/index.php/controller_name/method_name';
$config['total_rows'] = $this->your_model->get_total_rows(); // 獲取數(shù)據(jù)總數(shù)
$config['per_page'] = 10; // 每頁顯示的數(shù)據(jù)條數(shù)
$this->pagination->initialize($config);
public function get_data($limit, $offset) {
$query = $this->db->get('your_table', $limit, $offset);
return $query->result();
}
$data['results'] = $this->your_model->get_data($config['per_page'], $this->uri->segment(3));
$this->load->view('your_view', $data);
echo $this->pagination->create_links();
通過以上步驟,就可以在CodeIgniter中實(shí)現(xiàn)簡單的分頁功能了??梢愿鶕?jù)實(shí)際需求對(duì)分頁樣式和配置參數(shù)進(jìn)行調(diào)整。