我有一個圖片庫;我想要做的是用於顯示圖像的簡單分頁;在每個頁面中我只顯示18個圖像! 所以,我想在我的控制器以下代碼:PHP Codeigniter中的分頁(該網址對下一頁無效)
$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我在於:每個時間18個圖像是基於URI->段示出的方式來處理。在情況下,如果你需要我的模型方法:
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幅圖像),我應該有兩頁,我有兩頁,但問題是當我點擊第二頁時,網址轉到..../index/18而不是..../index/1
可能是什麼問題?
我可能不清楚,所以如果您需要更多說明,請告訴我。
感謝
分頁沒問題,它會轉到'/ index/18',在你的查詢中使用'uri-> segment(3)'作爲'offset'來獲取下一組r esults。 –
對不起,我沒有得到你的意思,你能告訴更多的解釋謝謝:) – user385729
分頁會這樣工作:第一頁:'/ index'然後第二頁:'/指數/ 18',然後第三:'/指數/ 36'等。現在我明白了嗎? –