codeigniter
  • pagination
  • 2017-10-10 68 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.我已经创建了另一种方法和路线。它工作正常 –

    相关问题