0

我现在尝试在我的网络应用程序中使用分页,但它不起作用。当我单击由 创建的链接时$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,所以我真的无法删除。这意味着如果我使用分页,页码将是第二个参数。

4

1 回答 1

0

如果您打算使用 url 中的页码,您应该执行以下操作:

    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();
        }

}

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

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

于 2013-05-09T12:14:33.273 回答