简体   繁体   English

CodeIgniter-分页不起作用

[英]CodeIgniter - Pagination won't work

I'm now trying to use Pagination in my web app but it doesn't work. 我现在正在尝试在Web应用程序中使用分页,但是它不起作用。 When I click the links created by $this->pagination->create_links() , I get 404 page error instead. 单击$this->pagination->create_links()创建的链接时,出现404页面错误。

Here's my controller: 这是我的控制器:

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

The $id passed to the index is used to determine which project to view and is also used to view the project_logs for that project so I really can't remove. 传递给索引的$id用于确定要查看的项目,也用于查看该项目的project_logs,因此我确实无法删除。 This means that if I'll use pagination, the page number will be the second parameter. 这意味着如果我将使用分页,则页码将是第二个参数。

if you're planning to use page number from your url you should do somenthing like this: 如果您打算使用网址中的页码,则应该执行以下操作:

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

}

but i'm not quite sure about your logic, i use another logic, and i use offset in urls 但我不太确定您的逻辑,我使用其他逻辑,并且在URL中使用offset

try this tutorial i think it's a good starting point: http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-day-7-pagination/ 试试这个教程,我认为这是一个很好的起点: http : //net.tutsplus.com/tutorials/php/codeigniter-from-scratch-day-7-pagination/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM