Loss of URI segments when paging with CodeIgniter

I have a / payment interface where the user should be able to filter through the price range, bank and other things. These filters are the standard blocks of choice. When I submit the filter form, all the email data goes to another method called payments / search. This method performs validation, saves the post values ​​in the flashdata session, and redirects the user back to / payments by passing the name flashdata to the URL.

So, my standard links to pages without filters are exactly like this:

payments/index/20/ payments/index/40/ payments/index/60/ 

And if you submit the filter form, the returned URL is:

 payments/index/0/b48c7cbd5489129a337b0a24f830fd93 

It works just fine. If I change the zero for something else, it is just paginated. However, the only problem is that <1 2 3 4 β†’ page links do not retain the hash after page offset. CodeIgniter generates links to pages, ignoring this extra uri segment.

My uri_segment configuration is already set to 3:

 $config['uri_segment'] = 3; 

I cannot set uri_segment to 4 because this hash may or may not exist. Any ideas on how I can solve this? Is it mandatory for CI to have an offset as the last segment in the uri? Maybe I'm trying to use the wrong approach, so I'm all ears.

Thanks guys.

edit: Why am I passing the name flashdata through a uri that you might ask about? because it allows the user to open multiple browser tabs and perform different searches on each tab. For each filter, you create a new flashdata var.

+4
source share
3 answers

For users coming from Google only, here is the solution:

In the pagination configuration array, I changed base_url to this:

 $config['base_url'] = site_url('payments/index/' . $this->uri->segment(3) . '/' . $this->uri->segment(4)); 

Where segment (3) is my offset number and segment (4) is the hash. Thus, in this case, the hash may or may not exist, and pagination will still work.

Another approach may be the following: if you go to payments and there is no flash data identifier in the URL, it redirects the user to payments / search and creates an empty array with empty filters. Then it redirects back to payments / _NEW_FLASHDATA_ID_HERE_. That way, the URL will always contain the flashdata identifier in the URL, and you can have the pagination offset in the last segment of the URI, as usual.

+2
source

Why not set a hash as a session item?

 $this->session->set_userdata('session_flashdata_hash', $hash); 

The hash will be available until you cancel it.

 $this->session->unset_userdata('session_flashdata_hash'); 

Or until you destroy the session.

 $this->session->sess_destroy(); 
+1
source

Why don't you check if the hash exists and is the uri_segment value uri_segment on it?

+1
source

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


All Articles