Search URL and pagination

Hello,

i followed this tutorial: https://getkirby.com/docs/cookbook/search

It works… but: if i page arround in the results, enter a new search query and send the form… then the new results URL has still the ‘/page:2/’ parameter. So if the new results fill up just one pagination page i’ll end up on the error page for this URL.

It’s maybe because of the nacked <form> tag but adding <form method="post" action="<?= $page->url() ?>"> will give me results on page 1… but if i use the pagination buttons the next site is empty.

Any ideas how to fix this?

S.

Have you checked if the $pagination object has pages?

I used the controller from the tutorial with a adition for a total count:

<?php

return function($site, $pages, $page) {

  $query   = get('q');
  $results = $site->search($query, 'title|text')->sortBy('date', 'desc');
  $total   = $results->count();
  $results = $results->paginate(20);

  return array(
    'query'      => $query,
    'results'    => $results,
    'total'      => $total,
    'pagination' => $results->pagination()
  );
};

Yes, but I mean in your template (replace $articles with $results)

<?php if($articles->pagination()->hasPages()): ?>
<nav class="pagination">

  <?php if($articles->pagination()->hasNextPage()): ?>
  <a class="next" href="<?php echo $articles->pagination()->nextPageURL() ?>">&lsaquo; older posts</a>
  <?php endif ?>

  <?php if($articles->pagination()->hasPrevPage()): ?>
  <a class="prev" href="<?php echo $articles->pagination()->prevPageURL() ?>">newer posts &rsaquo;</a>
  <?php endif ?>

</nav>
<?php endif ?>
  <?php if($articles->pagination()->hasNextPage()): ?>
  <a class="next" href="<?php echo $articles->pagination()->nextPageURL() ?>">&lsaquo; older posts</a>
  <?php endif ?>

Yes, but the pagination links missing the q(uery) parameter so the next site is empty because of no query…hmmm

Oh, ok, I think you can add options to the pagination object: https://getkirby.com/docs/toolkit/api/pagination/construct

Do you have a example how to do this? :frowning:

I only have an example to change the variable:

$articles = $page->children()->paginate(20, array( 'variable' => $site->language()->code() == 'de' ? 'seite' : 'page' )); 

But if you look in the source code, there is also a method option. These are the defaults:

  public static $defaults = array(
    'variable'      => 'page',
    'method'        => 'param',
    'omitFirstPage' => true,
    'page'          => false,
    'url'           => null,
    'redirect'      => false,
  );

I tried to just add the query to the link like:

<?php if($results->pagination()->hasNextPage()): ?>
<a class="next" href="<?php echo $results->pagination()->nextPageURL() ?>?q=<?php echo esc($query) ?>">&lsaquo; older posts</a>
<?php endif ?>

<?php if($results->pagination()->hasPrevPage()): ?>
<a class="prev" href="<?php echo $results->pagination()->prevPageURL() ?>?q=<?php echo esc($query) ?>">newer posts &rsaquo;</a>
<?php endif ?>

but no luck… on page 2 it still works: the link is blog/search/page:2?q=crm but on page 3 the link is: blog/search/page:3/?q=crm?q=crm

Damn, now it works like explained in the tutorial… don’t know what the problem was… sorry. :confused: