2013-10-23 59 views
0

我有一個圖片庫;我想要做的是用於顯示圖像的簡單分頁;在每個頁面中我只顯示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

可能是什麼問題?

我可能不清楚,所以如果您需要更多說明,請告訴我。

感謝

+0

分頁沒問題,它會轉到'/ index/18',在你的查詢中使用'uri-> segment(3)'作爲'offset'來獲取下一組r esults。 –

+0

對不起,我沒有得到你的意思,你能告訴更多的解釋謝謝:) – user385729

+2

分頁會這樣工作:第一頁:'/ index'然後第二頁:'/指數/ 18',然後第三:'/指數/ 36'等。現在我明白了嗎? –

回答

2

分頁是好的,它會去/ 指數/ 18,使用URI-

段(3)在 查詢偏移獲取下一組 結果

1

我的理解是,你想下一個頁面索引/ 2,那麼指數/ 3等,而不是指數/ 18。默認Codeigniter會將URL添加到下一頁的開始位置,這意味着頁面將從數據庫中的記錄18開始。

我想你想要的是

$配置[ 'use_page_numbers'] = TRUE; 默認情況下,URI段將爲您正在分頁的項目使用起始索引。如果您希望顯示實際的頁碼,請將其設置爲TRUE。

http://ellislab.com/codeigniter/user-guide/libraries/pagination.html