Jump to content

Pagination & GET results


j00st
 Share

Recommended Posts

Hi all,

I've set up a filter on my product-page, which I then use to...filter my products!
– I've got pagination set up, and 30 items per page.
– When I active the filter it works perfectly (in my opinion).

 

Here's what I'm struggling with though:
When I'm on another page (filtered as well/or the total overview) and I put my GET request in for the filter,
it gives back the result, but still with the page-number there. In some cases, this is no problem – like a A-Z or Z-A filter,
but others (say, per location) I might have less pages.

Visual/code ref: (I DO have 3 pages of authors, but I don't have 3 pages from London)

url: books/page3?author=ascending
url: books/page3?studio=london

 

The current setup for my pages that get rendered are as follows:

$allbooks = $pages->find("template=book, sort=$sort, $q, $tagged, $select_studio, start=0, limit=$limit");

As you can see I have the start=0 in there, but I read that's for the start of the pagination, not so much where it'll drop me in the search results.
$q, $tagged and $select_studio are all empty values, unless they're returned from the GET request

To repeat it, in it's most simplest form:

When I click a filter, and a GET request is done, I want to 'reset' the page-number to 0, and get my results...

Perhaps I'm missing something obvious, but I'd be really grateful to have your input.

Link to comment
Share on other sites

$allbooks = $pages->find("template=book, sort=$sort, $q, $tagged, $select_studio, limit=10");

if ($input->get()->count() && !$allbooks->count() && $input->pageNum > 0) {
	$url = $page->url([
	      'http' => true,
	      'pageNum' => 0,
	      'data' => $input->get()->getArray()
	    ]);

			$session->redirect($url);
}

Not tested and may result in redirection loop, but just an idea. 

Tested, it works. 

  • Like 1
Link to comment
Share on other sites

13 hours ago, lokomotivan said:

Maybe u can try putting "./" in your filter form action link


./?studio=london

<a href="./?studio=london">London</a>

 

This. You can just use the page->url for the action of your form. No get param needed here.

Link to comment
Share on other sites

10 hours ago, Zeka said:

$allbooks = $pages->find("template=book, sort=$sort, $q, $tagged, $select_studio, limit=10");

if ($input->get()->count() && !$allbooks->count() && $input->pageNum > 0) {
	$url = $page->url([
	      'http' => true,
	      'pageNum' => 0,
	      'data' => $input->get()->getArray()
	    ]);

			$session->redirect($url);
}

Not tested and may result in redirection loop, but just an idea. 

Tested, it works. 

Thats not really needed for this problem. Why complicate things unessecary? This wont work when using a get param to filter result with i paginated results. Ones you can add with using input whitelist.

Link to comment
Share on other sites

@lokomotivan @Soma That's indeed a nice, fast, and easy way to get it working!
Unfortunately I've made my form & GET setup a bit to complicated – so if I want to do this, I'll have to restructure everything, which I'd rather not of course. But I'll definitely keep it in mind for the next Filter-run!!

So, now I'm working with @Zeka's code...I think I understand what's happening, but I'm having some issues with implementation.
For example, I've replaced:

if ($input->get()->count() && !$allbooks->count() && $input->pageNum > 0) {

with the following:

if (count($_GET) > 0 && $input->pageNum > 1){

...as I got an error on $input->get()->count() (...and I don't know why).
The !$allbooks->count() is to verify there's actually books in there, right? So I SHOULD put that one in there...

So far I've been using $input->get->(name of what I'm getting) so;
- $input->get->studio
- $input->get->title
- $input->get->author

When I use Zeka's code in the URL 'data' element, I get the following error:

Parse error: syntax error, unexpected '' (T_STRING), expecting ']' in /Users/JCN/Dropbox/Websites/publicationstudio/site/assets/cache/FileCompiler/site/templates/header.inc on line 268

As the $input->get()->count() also didn't work for me, I'm wondering if I'm missing something obvious, or if it has something to do with my settings?
If I remove the line with 'data' it works fine – but of course isn't filtering anything ? as it'll go to page 0 of allbooks, unfiltered.

Perhaps it's also useful to show how I have these $input->get->x set up in relation to my $allbooks:

if($input->get->author === 'ascending'){
    $sort = 'author';
}elseif($input->get->author === 'descending'){
    $sort = '-author';
}elseif($input->get->title === 'ascending'){
    $sort = 'title';
}elseif($input->get->title === 'descending'){
    $sort = '-title';		
}else{
    $sort = 'sort';
}

Looking forward to hearing your take on this!!

Link to comment
Share on other sites

Ah, and I completely overlooked you comment @Soma...
 

Quote

Thats not really needed for this problem. Why complicate things unessecary? This wont work when using a get param to filter result with i paginated results. Ones you can add with using input whitelist.

"This won't work when using a get param" ?

Does what I said above still make sense then?

Link to comment
Share on other sites

Make sure you have a filter form that action url is the $page->url. 

I don't know what the problem is really but it's all getting too complicated for what it really can be (dead simple).

Example filter form for a paginated result list :

<?php namespace ProcessWire;

$filter = "";

$form = $modules->InputfieldForm;
$form->attr("action", $page->url);
$form->attr("method", "get");

// select with sort options
$f = $modules->InputfieldSelect;
$f->attr("name", "sort");
$f->label = "Sort";
$f->addOptions(array(
	"-title" => "descending",
	"title" => "ascending"
));
$form->add($f);

// submit button
$f = $modules->InputfieldSubmit;
$f->attr("name", "filter");
$form->add($f);

// process form
if(count($input->get)){
    // processes and populates form fields
	$form->processInput($input->get);
	
	// if sort is not empty
	if($form->get("sort")->value) {
		// build filter and add to whitelist so it gets picked up by pagination
		$input->whitelist("sort", $sanitizer->selectorValue($form->get("sort")->value));
		$filter .= ",sort=" . $input->whitelist("sort");
	}
}

$result = $pages->find("template=basic-page, limit=2{$filter}");

$content .= "<h2>Show Results</h2>";

if(!$result->count) {
	$content .= "<p>no results</p>";
} else {
	foreach($result as $res){
		$content .= "<p>$res->title<br>$res->url</p>";
	}
	$content .= $result->renderPager();
}

$content .= $form->render();

 

  • Like 7
Link to comment
Share on other sites

Thanks @Soma
I'm going to go through your code – as you said, should be able to do this 'dead simple' ?

Probably made it too difficult for myself along the way, so I'll have a look, and will sort this out.

Thanks for all the input everyone!

Link to comment
Share on other sites

  • 2 years later...

I have a similar issue, what I basically need is to reset the URL-segments, i.e. go back to the baseURL(?) without /page2 or above whenever the get variables change. Because when I'm on /page2 I and switch the search term, it won't find anything because the results I would get are not enough to require a pagination. 

You would think that something like this would be dead simple, but it's not. I don't even know what I exactly need to change. linkMarkup? urlSegment? pageNum? 

So here's how I tried to solve this now

if ($session->what != $what || $session->letter != $letter) {
     $input->setPageNum(0);
}
$session->letter = $letter;
$session->what = $what;

I tried many different API commands, but they all won't work. 

Please help!

Link to comment
Share on other sites

Step 1: Change your form element’s action attribute to <?php echo $page->url; ?>
Step 2: There is no step 2.

 

edit: sorry, I didn’t read the whole thread either. This same answer had actually been posted by Soma two posts ago.

  • Like 2
Link to comment
Share on other sites

18 hours ago, Jan Romero said:

Step 1: Change your form element’s action attribute to <?php echo $page->url; ?>
Step 2: There is no step 2.

 

edit: sorry, I didn’t read the whole thread either. This same answer had actually been posted by Soma two posts ago.

true. I missed that somehow, was more focused on the code. Anyways, that did the trick, thanks a lot, indeed dead simple ?

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...