【发布时间】:2013-04-28 21:11:37
【问题描述】:
在 CI 的分页中,数据索引应该跟随偏移量。例如:如果限制是 10,那么第二个索引应该有 10 个偏移量,这意味着索引将从 11 开始到 20。
我遵循了一些教程,但我仍然无法让这个偏移量正常工作。 每次点击不同的分页索引时,我的索引总是重置为 1。
这是我的分页代码,注意我试图回显 $offset 并且值为 true(在第二个索引 = 10,在第三个索引 = 20,$limit = 10) 所以我不知道为什么它不起作用:
public function index($offset = 0) {
//check authorization
if(!isset($_SESSION['username']))
redirect('backend_umat/login');
// the $offset value is true, but the index is still reseted to 1
echo $offset;
$limit = 10;
$result = $this->umat_m->get_umat($limit, $offset);
//pagination
$config['base_url'] = site_url('/backend_umat/index');
$config['total_rows'] = $result['num_rows'];
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
这是我的模型:
public function get_umat($limit, $offset) {
$this->db->select('*')->from('msumat')->limit($limit, $offset)->
join('mskelas', 'msumat.kelas_id = mskelas.kelas_id');
$q = $this->db->get();
$result['rows'] = $q->result();
$result['num_rows'] = $this->db->select('*')->from('msumat')->
join('mskelas', 'msumat.kelas_id = mskelas.kelas_id')
->get()->num_rows();
return $result;
感谢您的帮助:D
【问题讨论】:
-
我看不到你的代码中的错误,如果偏移量是正确的,你的查询呢?
echo $this->db->last_query()就在模型中的第一个查询之后。有什么问题,来自 DB 或分页 html 的数据?