在Codeigniter分页中替换get参数?

时间:2017-06-09 01:37:53

标签: php codeigniter pagination

我的分页功能的以下代码如下: -

public function news()
    {
        $this->load->library('pagination');
        $config = array();

        $config["base_url"] = base_url() . "index.php/welcome/news";
        $this->load->model('news_model');
        $total_row = $this->news_model->record_count();
        $config["total_rows"] = $total_row;
        $config["per_page"] = 1;
        $config['use_page_numbers'] = TRUE;
        $config['num_links'] = $total_row;
        $config['cur_tag_open'] = '&nbsp;<a class="current">';
        $config['cur_tag_close'] = '</a>';

        $config['page_query_string'] = TRUE;
        $config['next_link'] = 'Next';
        $config['prev_link'] = 'Previous';
        $config['first_url'] = $config['base_url'].'?'.http_build_query($_GET);
        $this->pagination->initialize($config);
        if($this->uri->segment(3)){ 
        $page = ($this->uri->segment(3)) ;
        }
        else{ 
        $page = 1;
        } //echo $config["per_page"].'/'.$page; exit();
        $this->load->model('news_model');
        $data["results"] = $this->news_model->fetch_data($config["per_page"], $page); 
        $str_links = $this->pagination->create_links();
        $data["links"] = explode('&nbsp;',$str_links );

        $this->load->model('news_model');
        $data['lt_news'] = $this->news_model->get_lt_newsletter();
        $data['rm_news'] = $this->news_model->get_rm_newsletter();        
        $this->load->view('newsletter/newsletter',$data);
    }

从上面的代码中,url浏览器显示如下: -

http://localhost/ins/index.php/welcome/news?per_page=2

我喜欢如何改变它看起来如下:

http://localhost/ins/index.php/welcome/news/2

有办法吗?我是codeigniter中的新手,所以,我不知道是否有必要更改url参数,看起来像上面那样..?

2 个答案:

答案 0 :(得分:2)

$config['page_query_string']设为false。

来自文档https://www.codeigniter.com/userguide3/libraries/pagination.html#customizing-the-pagination

  

默认情况下,分页库假设您正在使用URI段,   并构建您的链接:

     

http://example.com/index.php/test/page/20如果有的话   $ config ['enable_query_strings']设置为TRUE你的链接   使用查询字符串自动重写。这个选项也可以   明确设置。使用$ config ['page_query_string']设置为TRUE,   分页链接将成为:

     

http://example.com/index.php?c=test&m=page&per_page=20

答案 1 :(得分:1)

使页面成为函数的参数,如下所示:

public function news($pageNum)
    {
        ...
        $page = $pageNum
        ...
    }

然后您应该可以通过以下方式访问它:

http://localhost/ins/index.php/welcome/news/2