Jump to content

Pagination and urlSegments issue

Recommended Posts


I have a problem getting pagination working with urlSegments. I read the docs and the forum, and another issue comes up with sort of the same thing. The problem there was that baseUrl got set by options and was left empty, while in the code below, the baseUrl is first set by the module manager (or something deep down..)

Anyhow, the problem is: my urlSegments wont get rendered in the link. I have the template setup correctly and the strange thing is, only the first part of the urlSegments shows up. Strange eh?

All the others are added via $queryString I think because I added them to $input->whitelist();

But I'm not sure if this is necessary.

Is it correct that normally, it would work out of the box because the urlSegments are part of the baseUrl?

And, if I read the code in MarkupPageArray.module line 146, where the baseUrl is set by default, I read:

if(empty($options['baseUrl'])) $pager->setBaseUrl($this->page->url . ($this->page->urlSegment ? $this->page->urlSegment . '/' : '')); 

This seems wrong and the reason only the first urlSegment gets added. Or, should $page->urlSegment contain all segments? And if it doesn't what could be the problem?

(btw: maxUrlSegments=4, default)

Share this post

Link to post
Share on other sites

I looked at where $page->urlSegment gets retrieved, and I think for the pageNav functionality it should be done different.

it reads: (Page.php, line 545)

case 'urlSegment':
		$value = $this->fuel('input')->urlSegment1; // deprecated, but kept for backwards compatibility

Since it says deprecated, I made a modification to markupPageArray.module line 146:

if(empty($options['baseUrl'])) $pager->setBaseUrl($this->page->url . ( count($this->input->urlSegments)>0 ? '/'.implode($this->input->urlSegments,"/") . '/' : ''));

Replacing $this->page->urlSegment to $this->input->urlSegments, which seems more proper to me.

Should I file a bug report via Github and fork and make a pull request Ryan, or what is the best way to fix this? Or am I just messing things up by thinking this is the error while its not? :)

  • Like 1

Share this post

Link to post
Share on other sites

Thanks jtborger, that makes sense to me. I'm updating it to this:

if(empty($options['baseUrl'])) {
  $baseUrl = $this->page->url;
  $urlSegmentStr = $this->input->urlSegmentStr; 
  if(strlen($urlSegmentStr)) $baseUrl = rtrim($baseUrl, '/') . "/$urlSegmentStr/";

Share this post

Link to post
Share on other sites

Ah, I didn't know there was an $urlSegmentStr but that seems the right one to use indeed.

Ok problem solved then.


Share this post

Link to post
Share on other sites
Ah, I didn't know there was an $urlSegmentStr but that seems the right one to use indeed.

It's a pretty recent addition. I think it's in 2.3 stable. But if not, it's the same thing as implode('/', $input->urlSegments); 

Share this post

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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By jacmaes
      I have a series of videos, and the following search form (translated into English here) that allows to filter these videos on the frontend:

      I've built a few of these search forms, but only with text fields, selects and radio buttons. Here I'm using an array with checkboxes ("Level" field above), and it's causing me grief when I try to paginate these results. I've done a lot of searching in the forum and spent too many hours to try to get it to work. Here's how I'm building the selector:
      <?php if(count($input->get)): // Level is an array. Code adapted from Ryan's snippet here: // https://processwire.com/talk/topic/3472-enable-pagination-for-search-results/?tab=comments#comment-38042 if($input->get->level) { $level = array(); foreach($input->get->level as $id) $level[] = (int) $id; // sanitize to INTs $level = implode('|', $level); // convert to 123|456|789 string, ready for selector } else { $level = ''; } $data = array( 'training_type' => array('=', (int) $input->get->training_type), 'duration' => array('=', (int) $input->get->duration), 'level' => array('=', $level), 'limit' => array('=', (int) $input->get->limit) ); $selector = ''; // iterate through the $data we made above to create a selector string foreach($data as $field => $a) { list($operator, $value) = $a; if(empty($value)) continue; // send value to the whitelist so that it can be used in pagination $input->whitelist($field, $value); // append to our selector string $selector .= "$field$operator$value, "; } $videos = $page->children("$selector"); When I hit search, I get the expected results. So far so good. The GET parameters are the following with the options selected in the screenshot above:
      videos/?level[]=1476&level[]=1477&training_type=1473&duration=1485&limit=10 $selector echoes the following as the "level" field is an array with a pipe character:
      level=1476|1477, training_type=1473, duration=1485, limit=10 Now, when paginating these results, the following page (page 2) shows these GET parameters:
      videos/page2/?level=1476|1477&training_type=1473&duration=1486&limit=10 And I think that's where the problem lies. The "level" field is "lost" and I'm getting more results than expected on subsequent pages. If I manually add "page2" to the initial results in the URL, just to test, everything works fine:
      videos/page2/?level[]=1476&level[]=1477&training_type=1473&duration=1486&limit=10 But how can I achieve this in code? Do I need to revert to "level[]=1475&level[]=1477" instead of "level=1476|1477" for the pagination to work correctly, and can you PHP gods illuminate me?
      Any help would be really appreciated, really.
    • By christophengelmayer
      Hi everyone,
      I'm working on a CLI script that renders paginated pages.
      Therefore I iterate trough paginated pages and set the page number and render the result.
      My problem is, after calling the render function for the first time, the output doesn't change even if I change the page number.
      I'm using a template that renders a pagination of its children:
      <?php echo $page->children("limit=3")->render(); ?> I can view the paginated results in the Browser:
      /page-rendering-pagination/ /page-rendering-pagination/page2 /page-rendering-pagination/page3 ... When trying to render the different pages using the API I always get the first result, even if I change the page Number.
      <?php namespace ProcessWire; include('./index.php'); wire('input')->setPageNum(1); $p = wire('pages')->get('/page-rendering-pagination'); var_dump($p->render()); // renders first three items wire('input')->setPageNum(2); var_dump($p->render()); // also renders the first three items Am I missing something? Is there some kind of caching mechanism that I'm not aware of?
      Thanks for your help.
    • By dragan
      Quick question: Is it possible to use PW's pagination for other things than PageArrays? i.e. query custom database tables and paginate the results? Has anyone ever tried it?
    • By brdje
      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!
  • Create New...