Jump to content

Pagination with get vars


Recommended Posts

Hi, I am having problems controlling the pagination in ProcessWire. More specifically, how it displays the url. Normally, '/page2/' etc is appended to the url when using pagination. However, I am seeing all of my get vars (used for a search form) appended as well eg: '/page2/?dropdown_select_1=&dropdown_select_2='.

I am using $input->whitelist facility to build a search form that exists on all pages on the site. I am not sure how to clear this specifically for the pagination url to look cleaner.

My code fore the pagination is as follows:

$pagination = $articles->renderPager( array( 
		'nextItemLabel' => 'next', 
		'previousItemLabel' => 'prev',
		'listMarkup' => "<ul class='pagination'>{out}</ul>",
		'itemMarkup' => "<li class='{class}'>{out}</li>",
		'linkMarkup' => "<a href='{url}'><span>{out}</span></a>",
		'currentItemClass' => 'current',
		'getVars' => null

As you can see, I have tried to use the 'getVars' property to (incorrectly it seems) clear the get variables.

I'm using ProcessWire 2.2.9

Link to comment
Share on other sites

This is the normal behaviour if you want search options via GET. Adding your get vars to the whitelist makes the pagination work with those vars.

If you don't want to see the variables, use POST for your search form.

  • Like 1
Link to comment
Share on other sites

Thanks for the response Wanze.

Unfortunately, I'm not sure this is an option for me because the get vars used in the search form allow links to be setup to deep linked search form queries. For example: 'domain.com/search?dropdown1=test1&dropdown2=test2' would take a user to a page showing only the correct content items. This wouldn't work with post vars? Unless I'm just beeing a noob?

Just to be clear I'm not paginating the search form queries in this particular case, it's just that the search form is on every page, so it seems that I can't avoid having the variables showing whether they are needed for the pagination or not.

Link to comment
Share on other sites

you're welcome,

can you share some code snippets? I'm not sure if I understand what you are doing. :)

If you have a search form on your site, you have set an action=xy attribute. If you are using get, then all your fields are appended to the url.

Using post, this is not the case. If you don't need to paginate your search queries, I'm sure you could use post?

For example:

<form action="your-page" method="post">
<select name="dropdown1">
<select name="dropdown2">

//In your template where the form is sended
$dropdown1 = $sanitizer->selectorValue($input->post->dropdown1);
$dropdown2 = $sanitizer->selectorValue($input->post->dropdown2);
$results = $pages->find(""); //Use the post values to find your content

But now you have a problem when you want paging the results, because when clicking on a page number the form doesn't fire anymore. This is where the get vars are appended to the /pagexy link from the pager, allowing you to paginate your custom results. This only works if you add the get vars to the whitelist oder specify them in the options.

Link to comment
Share on other sites

  • 8 years later...
6 hours ago, Jonathan Lahijani said:

How do I set the exact portion in bold above into MarkupPagerNav's getVars / make that part automatically be part of the pagination links?

GET variables are automatically added to MarkupPagerNav links when they are set to $input->whitelist(). In this case you are dealing with an array value, so the input variables should probably be passed through $sanitizer->array() first before you do anything else with them.

$y = $sanitizer->array($input->get('y')); // There are also other ways you can apply sanitizers to input variables

If you do that then the sanitizer will give you an an array value from input in the format of "y[]=bar&y[]=baz" or "y=bar,baz" (these should be encoded when in a URL though). Then you would set:

$input->whitelist('y', $y);

By default MarkupPagerNav uses the comma separated form in its links, but if you prefer the square brackets then you can set:

echo $results->renderPager(['arrayToCSV' => false]);

But you won't get the exact format you showed because square brackets are typically encoded in URLs.

  • Like 4
  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...