我有一个图片库;我想做的是一个简单的分页来显示图像;在某种程度上,我在每一页中只显示 18 张图片!所以我在我的控制器中尝试了以下代码:
$this->load->model('Gallery_model');
$data['images'] = $this->Gallery_model->get_images(18, $this->uri->segment(3));
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/Gallery/index.php/gallery/index/';
$config['total_rows'] = count($this->Gallery_model->get_all_images());
$config['per_page'] = 18;
$this->pagination->initialize($config);
$data['main_content'] = "gallery";
$this->load->view('includes/template', $data);
在我的 Gallery_model 中,我以每次基于 uri->segment 显示 18 张图像的方式进行处理。如果您需要我的模型方法:
public function get_images($per_page, $segment) {
$files = scandir($this->gallery_path . "\output");
$newFiles = array_diff($files, array('.', '..', 'thumbs'));
$images = array();
foreach ($newFiles as $file) {
$images[] = array(
'url' => $this->gallery_path_url . 'output/' . $file,
'thumb_url' => $this->gallery_path_url . 'output/' . $file,
);
}
$newImage = array();
if ((($segment * $per_page)+$per_page) < count($images)) {
for ($i = 0; $i < $per_page; $i++) {
$newImage[$i] = $images[($segment * $per_page) + $i];
}
}else
{
for ($i = 0; $i < count($images)-($segment * $per_page); $i++) {
$newImage[$i] = $images[($segment * $per_page) + $i];
}
}
return $newImage;
}
public function get_all_images() {
$files = scandir($this->gallery_path . "\output");
$newFiles = array_diff($files, array('.', '..', 'thumbs'));
$images = array();
foreach ($newFiles as $file) {
$images[] = array(
'url' => $this->gallery_path_url . 'output/' . $file,
'thumb_url' => $this->gallery_path_url . 'output/' . $file,
);
}
return $images;
}
现在一切都很好,我有 25 张图片(超过 18 张图片),我应该有两页,但我确实有 2 页,但问题是当我点击第二页时,网址转到...。 /index/18 而不是 ..../index/1
可能是什么问题?
我可能不清楚,所以如果您需要更多说明,请告诉我。
谢谢