Jump to content

Losing parameters when using pagination


Liam88
 Share

Recommended Posts

Hi,

After years of just playing around with Processwire I have asked 3 q's in the same week. It's all about working with forms, parameters etc and so I'm hoping this ordeal is nearly over!

I currently have a checkbox filter:

<form id="abFilter" method="get" role="form" action="'.$page->url().'">
	<div class="list-group">
	<h3>Content Type</h3>';
	$cont = $fields->get('ab_content');
	$contents = $cont->type->getOptions($cont);
	foreach($contents as $ab_cont){
		echo'
		<div class="list-group-item checkbox">
		<input type="checkbox" class="" id="'.$ab_cont->title.'" name="content" value="'.$ab_cont->title.'"';
			if (in_array($ab_cont->title, $contArray)){
			echo "checked";
			}
		echo'>
		<label for="'.$ab_cont->title.'">'.$ab_cont->title.'</label>
		</div>';
	}
	echo'
	</div>';
//end of filter 1
//start of filter 2
echo'
	<div class="list-group">
	<h3>Channels</h3>';
	$chan = $fields->get('ab_channels');
	$channel = $chan->type->getOptions($chan);
	foreach($channel as $ab_chan){
		echo'
		<div class="list-group-item checkbox">
		<input type="checkbox" class="" id="'.$ab_chan->title.'" name="channel" value="'.$ab_chan->title.'"';
			if (in_array($ab_chan->title, $chanArray)){
			echo "checked";
			}
		echo'>
		<label for="'.$ab_chan->title.'">'.$ab_chan->title.'</label>
		</div>';
	}
	echo'
	</div>';
?>
<button id="select">Get Checked Checkboxes</button>
</form><!-- end of form -->

I also have a piece of script which selects all the checkboxes and then outputs them into readable parameters for the URL which then passes into the $inputs. The reason for the script is to not have duplicate filters like ?ab=1&ab=2 and the script changes it to ab=1_2 which on the input gets exploded into an array. 

document.querySelector("form").onsubmit=ev=>{
	ev.preventDefault();
	let o={};
	ev.target.querySelectorAll("[name]:checked").forEach(el=>{
	(o[el.name]=o[el.name]||[]).push(el.value)})
	console.log(location.pathname+"?"+Object.entries(o).map(([v,f])=>v+"="+f.join("_")).join("&"));
	document.location.href = location.pathname+"?"+Object.entries(o).map(([v,f])=>v+"="+f.join("_")).join("&");
	}

Here is $inputs and so on on the page:

//Default selector to get ALL products
$baseSelector = "template='adbank_pages',sort=published,include=all,status!=hidden,limit=2";
$selector = "template='adbank_pages',sort=published,include=all,status!=hidden,limit=2";

$input->whitelist('channel',explode("_", $channel));

// Use this to append to the $items filter

if($channel){
  $chanArray = explode("_", $channel);
  $chan = $channel = str_replace('_', '|', $channel);
  $selector = $selector .= ",ab_channels=$chan";
}

$test = $pages->find($selector);
// This is just testing if the $selector choise returns and if not use page filter without filters.

if(count($test) > 0){
	$items = $pages->find($selector);

	// $items with the parameter filter
	// Example - "template='adbank_pages',sort=published,include=all,status!=hidden,limit=2,ab_channels=facebook-ads"
	// Example (multi choice) - "template='adbank_pages',sort=published,include=all,status!=hidden,limit=2,ab_channels=facebook-ads|instagram-ads"
	// Example (with other filters) - "template='adbank_pages',sort=published,include=all,status!=hidden,limit=2,ab_channels=facebook-ads,ab_content=video|static"

}else{
	$items = $pages->find($baseSelector);
	// Example - "template='adbank_pages',sort=published,include=all,status!=hidden,limit=2"
}
$total = $items->getTotal();

I have stripped out a few of the other filters from the above to try keep it a little more concise (haha). Now I appreciate the post may be long but here we are at the end!

The URL I get on page 1 of the filter results would look like: example.com/blog/?channel=facebook-ads_instagram-ads

If I click page 2 the url changes to - example.com/blog/page2/?channel=

If I then click back to page 1 it changes to - example.com/blog/?channel=

So I'm hoping you can see my problem and hoping someone can assist. I need to work out how to keep the parameters in the url but also if I remove that filter for that parameter to remove.

This whole process works without pagination but with pagination it has a different behaviour.

Thank you in advance

Link to comment
Share on other sites

SOLVED:

So I added in the whitelisting just after the $input get.

if($input->get->channel){
	$channel = $input->get->text('channel');
	$input->whitelist('channel', $channel);// This is what I was missing.
	$chanArray = explode("_", $channel);
	$chan = $channel = str_replace('_', '|', $channel);
	$selector = $selector .= ",ab_channels=$chan";
}

I had the whitelist but needed to add it after the $input->get.

I also added $input->get->text('') to help sanitize according to the docs.

  • Like 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
 Share

×
×
  • Create New...