我試圖在不使用框架的分頁庫的情況下對Codeigniter 3應用進行分頁。更確切地說,我想分頁約100行。爲此:Codeigniter 3:分頁只顯示前10條記錄
在家用控制器我有:
public function index() {
$this->load->model('Customer');
$this->load->library('pagination');
$config = [
'base_url' => base_url("index.php"),
'per_page' => 10,
'total_rows' => $this->Customer->get_num_rows(),
'uri_segment' => 3,
'first_tag_open' => '<li>',
'first_tag_close' => '</li>',
'last_tag_open' => '<li>',
'last_tag_close' => '</li>',
'full_tag_open' => '<ul class="pagination">',
'full_tag_close' => '</ul>',
'next_tag_open' => '<li>',
'next_tag_close' => '</li>',
'prev_tag_open' => '<li>',
'prev_tag_close' => '</li>',
'num_tag_open' => '<li>',
'num_tag_close' => '</li>',
'cur_tag_open' => '<li class="active"><a>',
'cur_tag_close' => '</a></li>',
];
$this->pagination->initialize($config);
$customers = $this->Customer->getCustomers($config['per_page'], $this->uri->segment($config['uri_segment']));
$this->load->view('home', ['records'=>$customers]);
}
在模型文件中我有:
class Customer extends CI_Model {
public function __construct() {
$this->load->database();
}
public function getCustomers($limit, $offset) {
$this->db->limit($limit, $offset);
$query = $this->db->get('customers');
return $query->result();
}
}
Finaly,認爲:
<div class="pagination-container text-center">
<?php echo $this->pagination->create_links(); ?>
</div>
但如果我點擊第二頁,或任何其他頁面上,我得到相同的前10個記錄如可以在下面的圖片中可以看出:
2網頁的網址是:
http://localhost/cicrud/index.php?page=2
我做錯了什麼,我不明白是什麼。任何幫助?謝謝!
如果你使用的控制器是cicrud,那麼你的base_url不應該是/。此外,uri_segment大概是2,而不是5. –
在公共函數索引($ offset = 0)中傳遞偏移量作爲默認值 –
也在模型函數getCustomers($ limit = null,$ offset = null){} –