Jump to content
j00st

Pagination & GET results

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.

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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!!

Share this post


Link to post
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?

Share this post


Link to post
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 6

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Mithlesh
      Changing it to null
    • By MateThemes
      Hello everyone!
      I have a problem with pagination. I have following code:
      <div class="uk-section"> <?php $results = $pages->find("template=gallery, limit=10, sort=title"); $pagination = $results->renderPager(); echo $pagination; echo "<ul class='uk-pagination uk-flex-center'>"; foreach($results as $result) { echo "<li><a href='{$result->url}'>{$result->title}</a></li>"; } echo "</ul>"; echo $pagination; ?> </div> Which works fine. And this markup results in following html code:
      <div class="uk-section"> <ul class='uk-pagination uk-flex-center'> <li> <a href='/galerie-bisheriger-projekte/'>Galerie bisheriger Projekte</a> </li> <li> <a href='/galerie-bisheriger-projekte/galerie-1/'>galerie-1</a> </li> </ul> </div> But instead of "Galerie bisheriger Projekte" and "galerie-1" I want to have a number.
      And as last question, how can I add a previous and next button?
      Thanks for your help!
    • By Tyssen
      I have a page that contains a single ProFields table field and I want to display the contents of the table on the front end and then for logged in users, they can edit certain columns in the table.
      What I have at the moment is
      $out = '<form action="'.$page->url.'" method="post" > <table class="table"> <tbody>'; $count = 1; foreach($page->fieldName as $row) : $out .= ' <tr> <td><input type="checkbox" name="fieldName_'.$count.'_columnName"></td> </tr>'; if($input->post->submit) : $page->of(false); $page->set('fieldName_'.$count.'_columnName', $sanitizer->text($input->post->{fieldName_'.$count.'_columnName})); $page->save(); endif; $count++; endforeach; $out .= ' </tbody> </table> <button class="button" type="submit">Save</button> </form>'; The two problems I have are:
      I get an error trying from $sanitizer->text($input->post->{fieldName_'.$count.'_columnName}), not sure how to make that dynamic.  If I change the above to just a static value, e.g. $page->set('fieldName_1_columnName', 'Testing') and save the form, it's not saving the values to the database. Where am I going wrong?
    • By Liam88
      Hi all,
      Quick question as I haven't found anything from my Googling. 
      I have a blog on the site which utilises pagination. On the standard blog page I have a custom header which has featured posts.
      Below that I then have recent posts like mosts blogs.
      Now if i click to page two or three I want the header to disappear and just show a continuation of the standard posts.
      I'm not sure how to go about this so any direction would be helpful.
      Thanks
    • By VeiJari
      Hello forum. I'm trying to figure out how to detect if current form has errors after saving it in pw admin page. This is so that our event location isn't added in database if we detect errors. But I can't seem to find a correct way to detect errors? I only found the wire()->errors() and it always triggers even if I don't have any errors. 
      How can I detect errors in a form?
      By error I mean $page->error('this is the red error');
×
×
  • Create New...