CodeIgniter分页不起作用:找不到对象!错误404

时间:2014-03-28 09:57:59

标签: php codeigniter

我正在为codeigniter进行分页。我能够在我的页面上设置它,但是当我点击下一页时它会说

  

找不到对象!错误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');

2 个答案:

答案 0 :(得分: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页中,因为您没有在模型函数中传递限制。因为您在/

结束时忘记了base_url,所以获得了404

在模型中

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

答案 1 :(得分:0)

使用必须编写路由以获取页码的值。您必须将此行添加到application / config文件夹

中的routes.php

代码: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');

}