【问题标题】:CodeIgniter - Pagination won't workCodeIgniter - 分页不起作用
【发布时间】:2013-05-10 22:21:14
【问题描述】:

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

【问题讨论】:

    标签: php codeigniter pagination


    【解决方案1】:

    如果您打算使用网址中的页码,您应该这样做:

        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/

    【讨论】:

    猜你喜欢
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    相关资源
    最近更新 更多