Deepak Keynes
Deepak Keynes

Reputation: 2329

Replace get parameters in Codeigniter pagination?

The following piece of code for my pagination function like following:-

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

From the above code, the url browser shows like the following:-

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

I am like jammed as to how to change it to be look like the following:

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

Is there a way to do it..? I am newbie to the pagination in codeigniter so, i do not know if there is a necessity to change the url parameters to be looking like the above..?

Upvotes: 2

Views: 667

Answers (2)

Chris Lam
Chris Lam

Reputation: 3614

Set $config['page_query_string'] to false.

From the doc https://www.codeigniter.com/userguide3/libraries/pagination.html#customizing-the-pagination:

By default, the pagination library assume you are using URI Segments, and constructs your links something like:

http://example.com/index.php/test/page/20 If you have $config['enable_query_strings'] set to TRUE your links will automatically be re-written using Query Strings. This option can also be explicitly set. Using $config['page_query_string'] set to TRUE, the pagination link will become:

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

Upvotes: 2

Thomas James Tiam-Lee
Thomas James Tiam-Lee

Reputation: 691

Make the page a parameter of the function, like so:

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

Then you should be able to access it via:

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

Upvotes: 1

Related Questions