为codeigniter分页获取404错误

时间:2015-05-03 17:18:53

标签: php codeigniter pagination

这是我的班级:

class Pacients extends CI_Controller {
    public function __construct(){
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->library('email');
        $this->load->library('session');
        $this->load->library('pagination');

        $this->load->model('pacient_model');
        $this->load->model('department_model');
    }
    public function index(){
        $id_doctor = $this->session->userdata('userid');
        $department = $this->department_model->get_department_by_doctor($id_doctor); // get deparmentd_id
        $diagnostics = $this->department_model->get_diagnostics($department[0]->department_id); // get diagnostics by using deparment id 
        $department_data = $this->department_model->get_department_by_id($department[0]->department_id); // get all info about deparment

        //start pagination 
        if ($this->uri->segment(2) == null || $this->uri->segment(2) == ""){
            $limit = 0;
        }else{
            $limit = $this->uri->segment(2);
        }
        $get_pacients = $this->pacient_model->get_pacients_by_doctor($id_doctor, $limit);
        $total = $this->pacient_model->get_total_pacients_by_doctor($id_doctor);
        $config['base_url']   = base_url().'pacients';
        $config['total_rows'] = $total[0]->total;
        $config['per_page']   = 3;
        $this->pagination->initialize($config);
        // end pagination
        $data = array(
            'pacients'        => $get_pacients,
            'diagnostics'     => $diagnostics,
            'department_data' => $department_data
        ); 
        if (!$diagnostics){// check if there any diagnostic in db associated to the current department
            $data['error'] = 1;// if it is 1 then a form will apears in the view
        }

        $this->load->view('frontend/common/header');
        $this->load->view('frontend/pacients',$data);
        $this->load->view('frontend/common/footer');
    } 
}

在get_pacients变量中,我得到了所有结果,并得到了所有想要的结果。在视图中向我展示了分页。但是当我点击第2页时它会显示404页面。这很奇怪,在另一个页面上,我使用相同的分页实现,它的工作原理。网址如下:

http://localhost/licenta/pacients  

你知道为什么我会得到404吗?我正在使用Codeigniter 3. Thx提前

1 个答案:

答案 0 :(得分:0)

在codeigniter中默认情况下,URI的第二段是类的方法。所以你必须改变你的分页变量,你必须做出这些改变。

$this->uri->segment(2);$this->uri->segment(3);

$config['base_url'] = base_url().'pacients/index';