我试图在不使用框架的分页库的情况下对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){} –