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 brdje
      Greetings,
       
      I've created a product database which all use the template product.php and are published on the website.
      I'm trying to create an overview table with pagination of all the products, but using $pages->get does not return an object, only the object title.
      $products = $pages->get("template=product, limit=10"); This returns 10 strings in the frontend but not the object. When looping over the result set to get product fields, i get the following error:
      Notice: Trying to get property 'title' of non-object
       
      Because the product database is fairly large, i've added a pagination using the same query and this does return all the pages, but i can't click on the links that the paginator renders.
      $results = $pages->find("template=product, limit=10"); if($results->getTotal() > 10) { echo $results->renderPager(array( "nextItemLabel" => "Volgende", "previousItemLabel" => "Vorige", "currentItemClass" => 'active' )); }
      I've added the option in the template to allow page numbers. When i navigate to the link manually, i still end up seeing the 10 first products.
       
      I've tried changing several settings around, but i'm a bit stuck on how to resolve these issues. Do you have any advice what I need to adjust?
      Thanks in advance for your feedback.
       
       
    • By Peter Troeger
      Hello Community 🙂
      Has anyone ever tried having multiple elements on one page that get their info with $pages->find('selector, limit=n') and tried using pagination on one of these elements without effecting the other?
      I have a slider on a page where I display content with pagination. But when I go to page two, the slider content also goes to page two, which I don't want it to do 🙂
      Any tips are greatly appreciated!
      Thanks!
      -Peter
    • By Peter Troeger
      Hello 🙂
      I have set up pagination on a mulitlanguage site.
      I've done this before, but this time I have a problem I can't solve.
      Pagination is activated on 'parent-template' and 'child-template' just to be sure 🙂
      This is my code:
      $children = $page->children('limit=1'); foreach($children as $child) { $title = $child->title; echo $title; } echo $children->renderPager(); The navigation is output correctly and the link look correct as well 'parent-page/page2/'.
      But when I click the link, the site seems to redirect back to 'parent-page/
      Any help would be greatly appreciated 🙂
      - Peter
    • By Moritz Both
      Greetings,
      when I give users the user-admin-* permission to administer users who have a certain role, they can indeed see and edit those users. However the filter / column panel does not show in the admin interface. How can I enable the filter functionality for user-admin-* members?
      ProcessWire 3.0.123
      Moritz
×
×
  • Create New...