codeigniter
  • pagination
  • 2017-10-10 64 views 2 likes 
    2

    我是Codeigniter的新手,我正在進行分頁。但是我的代碼中有錯誤。在下面的代碼中我做了什麼錯誤? 我附上了我的代碼片段。分頁在Codeigniter中無法正常工作

    路線

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

    控制器

    public function index($page='home') 
    
        { 
    
         if (! file_exists(APPPATH.'views/pages/'.$page.'.php')) 
         { 
         // Whoops, we don't have a page for that! 
         show_404(); 
         } 
    
    
    
         $config=[ 
    
          'base_url' => base_url().'/pages/index', 
          'per_page' => 5, 
          'total_rows' =>$this->products_model->record_count() 
         ]; 
    
         $this->pagination->initialize($config); 
    
         $data['title'] = ucfirst($page); // Capitalize the first letter 
         $data['products'] = $this->products_model->get_products($config['per_page'], $this->uri->segment(3)); 
    
         $data['request'] = 'pages/home'; 
         $this->load->view('templates/content',$data); 
        } 
    

    模型

    public function get_products($limit, $offset) 
    { 
        $this->db->order_by('id', 'DESC'); 
        $this->db->limit($limit, $offset); 
        $query=$this->db->get('product'); 
        return $query->result_array(); 
    } 
    
    public function record_count($value='') 
    { 
        return $this->db->count_all("product"); 
    } 
    

    六ew

    <?php echo $this->pagination->create_links() ?> 
    

    在視圖中,我顯示錶中的記錄。記錄顯示第一頁,但是當我點擊第二頁它顯示錯誤

    "**404 Page Not Found**" 
    

    任何形式的關於此問題的幫助將得到高度讚賞。提前致謝。

    +0

    您希望看到的記錄是什麼?是否有超過5個? – theUtherSide

    +0

    是我有30多條記錄,但是當我點擊第二頁的分頁時,頁面沒有找到錯誤顯示 –

    +0

    你可以發佈代碼也是你的按鈕?這可能是。或者,當您獲得404時,$ $ page的價值是多少?也許這個文件不存在。 – theUtherSide

    回答

    0

    從你的邏輯中,index「index($ page ='home')這一行index方法獲得腳本顯示從傳遞給它的參數。在第一次加載時,默認頁面'home.php'是匹配,但是當你選擇第二頁時,CI路由器和索引函數尋找可能是'1.php'或'2.php'等,這取決於你的分頁,這些文件是否存在你請求的每一頁?否則,您應該檢查頁面變量是否爲有效頁面(例如非負數),然後繼續執行並將結果加載到您的視圖中

    +0

    是的,你是對的@Peter M.我已經創建了另一種方法和路線。它工作正常 –

    相關問題