2014-03-28 47 views
0

我正在研究codeigniter的分頁。我可以把它在我的網頁,但是當我點擊下一頁,它會說CodeIgniter Pagenation不起作用

Object not found! Error 404. 

我,當我點擊了下,這是http://localhost/contents/index/2放置正確。另外我注意到的一點是$config['per_page'] = 2;當我加載頁面時,從數據庫中循環的所有數據都顯示在1頁而不是每頁2頁。

控制器

//Pagenation below  
      $this->load->library('pagination'); 
      $config['base_url'] = 'http://localhost/contents/index'; 
      $config['total_rows'] = 200; 
      $config['per_page'] = 2; 
      $this->pagination->initialize($config);  
      echo $this->pagination->create_links(); 

      $data['contents'] = $this->content_model->get_content(); 
      $data['title'] = 'Contents'; 
      $this->load->view('template/header', $data); 
      $this->load->view('pages/index', $data); 
      $this->load->view('template/footer'); 

回答

0

試試這個

  function index($offset=null){ 
       $this->load->library('pagination'); 
       $config['base_url'] = 'http://localhost/contents/index/'; 
       $config['total_rows'] = 200; 
       $config['per_page'] = 2; 
       $this->pagination->cur_page = $offset; 
       $this->pagination->initialize($config);  


       $data['contents'] = $this->content_model->get_content($config['per_page'],$offset); 
       $data['title'] = 'Contents'; 



       $this->load->view('template/header', $data); 
       $this->load->view('pages/index', $data); 
       $this->load->view('template/footer'); 
} 

整體數據顯示1頁,因爲你沒有在模型通過限制function.you得到404,因爲你在月底忘記/base_url

in model

function get_content($limit,$offset) { 

     $query = $this->db->get('content',$limit,$offset); 
     return $query->result(); 
    } 

鑑於

echo $this->pagination->create_links(); //put this in view not in controller 
+0

這是我主持人:缺少內容:: index()和未定義變量的參數1:offset – Katsune

+0

我的數據未顯示T_T,並且每當Iclick no 2或3時它顯示「Object not found Error 404」,.男人我討厭那個錯誤。 – Katsune

+0

你改變了route.php –

0

使用必須編寫路線得到頁碼值。你必須將此行添加到應用程序/ config文件夾中的routes.php文件

CODE:routes.php文件

$route['index/(:num)'] = 'index/$1'; 

指數函數的index.php:

function index($current_page){ 

$this->load->library('pagination'); 
      $config['base_url'] = 'http://localhost/contents/index'; 
      $config['total_rows'] = 200; 
      $config['per_page'] = 2; 
      $this->pagination->initialize($config); 
      $this->pagination->cur_page = $current_page; 
      echo $this->pagination->create_links(); 

      $data['contents'] = $this->content_model->get_content(); 
      $data['title'] = 'Contents'; 
      $this->load->view('template/header', $data); 
      $this->load->view('pages/index', $data); 
      $this->load->view('template/footer'); 

} 
+0

我試着給我一個錯誤,所以我給($ current_page = 1)添加一個值,結果是一樣的,都在一個,當我點擊2或3時,它會給我相同的本地主機鏈接我的帖子與錯誤「對象未找到錯誤404。 – Katsune