CodeIgniter - 分页不起作用

时间:2013-05-09 09:33:50

标签: php codeigniter pagination

我现在正在尝试在我的网络应用中使用分页,但它不起作用。当我点击$this->pagination->create_links()创建的链接时,我会收到404页错误。

这是我的控制器:

function index($id) {   

    $this->load->library('pagination');

    if($project = $this->projects_model->get($id)) {
        $temp = $this->project_logs_model->get_by('project_id', $project->id);

        //$config = array();
        //$config['base_url'] = base_url().'projects/'.$project->id.'/';
        $config['base_url'] = current_url();
        $config['total_rows'] = $temp->num_rows();
        $config['uri_segment'] = 3;
        $config['per_page'] = 5; 
        $config['first_link'] = 'Latest';
        $config['last_link'] = 'Oldest';
        $config["num_links"] = 2;
        //$config['use_page_numbers'] = TRUE;
        $this->pagination->initialize($config);

        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

        $this->check_access($project);

        $this->data->current_project = $project;
        $this->data->updates = $this->project_logs_model->get_by('project_id', $project->id, $config['per_page'], $page);
        $this->data->links = $this->pagination->create_links();

        $this->view('project-main', $this->data);
    } else {
        show_404();
    }
}

传递给索引的$id用于确定要查看的项目,还用于查看该项目的project_logs,因此我实际上无法删除。这意味着如果我将使用分页,页码将是第二个参数。

1 个答案:

答案 0 :(得分:0)

如果您打算使用网址中的网页编号,则应按照以下方式进行评估:

    function index($id) {   

        $this->load->library('pagination');
         $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
         $project = $this->projects_model->get($id);    
    if(count($project) > 0) {
            $temp = $this->project_logs_model->get_by('project_id', $project->id);

            //$config = array();
           $config['base_url'] = base_url().'projects/'.$project->id.'/'.$page;
           // $config['base_url'] = current_url();
            $config['total_rows'] = $temp->num_rows();
            $config['uri_segment'] = 3;
            $config['per_page'] = 5; 
            $config['first_link'] = 'Latest';
            $config['last_link'] = 'Oldest';
            $config["num_links"] = 2;
            //$config['use_page_numbers'] = TRUE;
            $this->pagination->initialize($config);

            $this->check_access($project);

            $this->data->current_project = $project;
            $this->data->updates = $this->project_logs_model->get_by('project_id', $project->id, $config['per_page'], $page);
            $this->data->links = $this->pagination->create_links();

            $this->view('project-main', $this->data);
        } else {
            show_404();
        }

}

但我不太确定你的逻辑,我使用另一种逻辑,我在网址中使用偏移量

尝试本教程我认为这是一个很好的起点:http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-day-7-pagination/