The first page, codenamed "Codeword" is always the current page

I pull my hair out on this one. I use the CodeIgniters break library and now it is always stuck on page 1 as the current page. I checked a lot of StackOverflow questions and I don't have the same problem as others.

Here is my url structure

website.com/leaders/page/[page_number] 

Here is the page code in my controller

  $this->load->library('pagination'); $config['per_page'] = $query_config['limit']; $config['base_url'] = base_url() . 'leaders/page/'; $config['total_rows'] = 2000; // I actually use a function for this number $config['full_tag_open'] = '<div id="paginate">'; $config['full_tag_close'] = '</div>'; $config['first_link'] = '&laquo; First'; $config['last_link'] = 'Last &raquo;'; $config['use_page_numbers'] = true; $config['uri_segment'] = 3; $this->pagination->initialize($config); 

When I repeat pagination in the view, it looks like this works. The URLs on each link are correct and everything looks fine. The last link shows the last URL of the page, and the current page is 1. However, when I click on page 2 or any other paginated page, it still shows page 1 as the current page, although the URL looks like this way

 website.com/leaders/page/2 

I used $this->uri->segment(3) to capture the page number for my database queries so that the page number is in the right segment. Just to double check, I set the values โ€‹โ€‹of $config['uri_segment'] to 1,2,3,4,5,6 to make sure.

I understood the problem when writing this, but I'm still confused

Then I thought maybe something is happening with the URL itself, since I have a route directing it to the index method in the controller. This is what my route file looks like

routes.php

 $route['leaders/page/(:num)'] = 'leaders/index'; $route['leaders/page'] = 'leaders/index'; 

Then I tried to set base_url for the pagination configuration to send it to the index right like this:

 $config['base_url'] = base_url . 'leaders/index'; 

Now it works correctly. But how can I make it work with the URL structure I had before? I just think it looks prettier, and for that I really don't need a method in the controller. Is there something conflicting in the routes.php file?

thanks

+6
source share
2 answers

define cur_page and define controller as:

 public function index($page=''){ //... $page = ($page!='')? $page : 0; $config["cur_page"] = $page; //... } 
+6
source

Use this in your code, hope this works -

 if ($this->uri->segment(3) > 0) { $offset = $this->uri->segment(3) * $config['per_page'] - $config['per_page']; } else { $offset = $this->uri->segment(3); } 
0
source

Source: https://habr.com/ru/post/954331/


All Articles