我使用分页对页面中的数据进行分页,这些数据是照片名称(ex: 11abf76631b0c9daf2284825c3b6ade7.jpg)
。
问题是在下一页经常显示照片
数据库中的总数据也是30个元素,这里显示40个元素。
第1页 的第2页
控制器: photos_album
$this->load-> library('pagination');
$config['base_url'] = base_url()."admin/photos_album/display";
$config['total_rows'] = $this->photos_album_model->count_all();
$config['per_page'] = 20;
$config['num_links'] = 5;
$config['uri_segment'] = 4;
$config['use_page_numbers'] = TRUE;
$config['full_tag_open'] = "<div id='pagination' class='clear'>";
$config['full_tag_close'] = "</div>";
$config['prev_link'] = "<span class='pe-7s-angle-right fontSize_20'></span>";
$config['prev_tag_open'] = "<div id='prev' class='float_right'>";
$config['prev_tag_close'] = "</div><div id='counter'>";
$config['cur_tag_open'] = "<div class='float_right current'>";
$config['cur_tag_close'] = "</div>";
$config['num_tag_open'] = "<span class='float_right'>";
$config['num_tag_close'] = "</span>";
$config['next_link'] = "<span class='pe-7s-angle-left fontSize_20'></span>";
$config['next_tag_open'] = "<div id='next' class='float_right'>";
$config['next_tag_close'] = "</div>";
$config['first_link'] = "";
$config['last_link'] = "";
$this->pagination->initialize($config);
$page = ($this->uri->segment(4)) ? $this->uri-> segment(4) : 0;
$data['db_data'] = $this->photos_album_model->get_all($config["per_page"], $page);
$data['pagination_links'] = $this->pagination->create_links();
型号: photos_album_model
public function get_all($limit, $start) {
return $this-> db-> limit($limit, $start)-> get('photo_album');
}
public function count_all() {
return $this->db->count_all("photo_album");
}
查看
<div class="main_block">
<div class="title"><span class="pe-7s-browser fontSize_18"></span> <h2 class="font_2"></h2></div>
<div class="content fontSize_18 font_1">
<div id="images" class="clear gallery clearfix">
<?php
if ($db_data->num_rows() > 0) {
foreach ($db_data->result() as $photo) {
?>
<div class="float_right"><a href="<?php echo base_url(); ?>assets/uploads/<?php echo $photo->image;?>" class="hyperlink" rel="photofly"><img src="<?php echo base_url(); ?>assets/uploads/thumbs/<?php echo $photo->image;?>" alt="" /></a><div class="tools"> <a href="#"><span class="pe-7s-note editBtn"></span></a><a href="<?php echo base_url() . 'admin/photos_album/delete_processing/' . $photo->photo_id ?>" onclick="return confirm('Do you want to delete this item ?')" ><span class="pe-7s-close-circle delBtn"></span></a></div></div>
<?php
}
}
?>
</div>
</div>
<?php echo $pagination_links; ?>
</div>
注意:我的分页页面从0开始而不是从1开始,我不知道这是否正确。
答案 0 :(得分:2)
问题的原因非常简单。
问题出在这个属性
更改强>
$config['use_page_numbers'] = TRUE;
要强>
$config['use_page_numbers'] = FALSE;
那就是它。
答案 1 :(得分:1)
你的逻辑错误在这里:
$page = ($this->uri->segment(4)) ? $this->uri-> segment(4) : 0;
$data['db_data'] = $this->photos_album_model->get_all($config["per_page"], $page);
...
public function get_all($limit, $start) {
return $this-> db-> limit( $limit, $start)-> get('photo_album');
}
您要向您的功能发送两个参数:每页的项目数和页码。但是,在您的方法中,您将它们视为限制(正确)和开始(错误)。
您需要以下内容:
public function get_all($limit, $page) {
return $this-> db-> limit($limit, ($page * $limit) )-> get('photo_album');
}
您还应添加固定的ORDER BY
子句,以确保每个请求的顺序相同。