Svnt
March 17, 2017, 2:52pm
1
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?
Svnt
March 17, 2017, 3:05pm
3
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() ?>">‹ older posts</a>
<?php endif ?>
<?php if($articles->pagination()->hasPrevPage()): ?>
<a class="prev" href="<?php echo $articles->pagination()->prevPageURL() ?>">newer posts ›</a>
<?php endif ?>
</nav>
<?php endif ?>
Svnt
March 17, 2017, 3:10pm
5
<?php if($articles->pagination()->hasNextPage()): ?>
<a class="next" href="<?php echo $articles->pagination()->nextPageURL() ?>">‹ 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
Svnt
March 17, 2017, 3:30pm
7
Do you have a example how to do this?
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,
);
Svnt
March 17, 2017, 4:06pm
9
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) ?>">‹ 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 ›</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
Svnt
March 17, 2017, 5:21pm
10
Damn, now it works like explained in the tutorial… don’t know what the problem was… sorry.