Jump to content

[Solved] Pagination out of search form results: How to start from first page


Federico
 Share

Recommended Posts

Hello PW community,

I've a search form which return selected items with pagination. Everything works fine, also the pagination display correctly all results through pages.

The issue is given only for selected results pagination as the latter does not start from page 1 but it revert back results from the current page (e.g. if you are in page 3, and submit a result from selects form, than all related items are displayed - not starting from page 1 but from page 3, which means something a fake "no results").

This is the classic function of render pager:

echo $works->renderPager(array('arrayToCSV' => false));

Any idea? Thanks!

 

Edit: I've also tried the "start=0" within the selectors, however it Always re-start from page 1 and stick to that, regardless. So no more option to move to page 2, 3, ....

Link to comment
Share on other sites

Thank you for the tip, I've tried the following combinations within the action="" brackets, with no luck (results from the search form should be displayed in the same page as per the form, page named "example"):

"/example/"

"/example"

"/"

"?"

"/?"

"?/"

""

 

All the above options brings back either an error for the pagination module, or filtered items but not starting from page 1

Link to comment
Share on other sites

I am basically call the pagination as a function, passing it the search form selectors as argument :

function pagination($selector) {
	$works = wire('pages')->find($selector);
	if(count($works)) {
		echo $works->renderPager(array('arrayToCSV' => false));
	}
}

the search form is essentially as described in a previous post: 

So maybe this issue is caused by the pagination function, as I am echoing it in the search form page as:

<?php pagination($selector); ?>

but i cannot figure it out why it doesn't restart from page 01 when user submit a search form..

Link to comment
Share on other sites

this is how i did it using functions:

 function paginateItems($items) {
        $items_pp = 5;
		$start = (wire('input')->pageNum - 1) * $items_pp;
		$total = count($items);
		$items = $items->slice($start, $items_pp);
		$a = new PageArray();
		foreach($items as $unused) $a->add(new Page());
		$a->setTotal($total);
		$a->setLimit($items_pp);
		$a->setStart($start);
		$items->data('paging', $a);
		return $items;
	}

	function renderPager($items) {
		$options = array(
		    'nextItemLabel' => 'Next &raquo;</i>',
		    'previousItemLabel' => "&laquo; Previous",
		    'listMarkup' => "<ul class='pagination'>{out}</ul>",
		    'itemMarkup' => "<li class='{class}'>{out}</li>",
		    'linkMarkup' => "<a href='{url}'><span>{out}</span></a>",
		    'currentItemClass' => 'active'
		);

		$out = '<ul class="pagination">';
		$out .= $items->renderPager($items, $options);
		$out .= '</ul>';
		return $out;
	}

in the template:

$a = $items->data('paging');
echo renderPager($a);

 

Link to comment
Share on other sites

Thank you for sharing this, it looks interesting. Sorry for my layman point of view, but what exactly $items as argument retains? I guess the pages with specific template, isn't it?

And when it comes to pass $selector as argument, which contains the submitted search form values, where should be located? Many thanks!

Link to comment
Share on other sites

6 hours ago, Federico said:

I am basically call the pagination as a function, passing it the search form selectors as argument :


function pagination($selector) {
	$works = wire('pages')->find($selector);
	if(count($works)) {
		echo $works->renderPager(array('arrayToCSV' => false));
	}
}

 

I think this is wrong - you don't want to be doing another $pages->find() inside your pagination function (I don't think you need to execute your pagination in a function, TBH).

The basic flow of your search template should be:

// sanitize and build your $selector from $input->get()
// ...

// find your search results
$results = $pages->find($selector);

// output your search results
foreach($results as $result) {
    //...
}

// render your pagination from the $results PageArray
echo $results->renderPager(array('arrayToCSV' => false));

 

  • Like 1
Link to comment
Share on other sites

I've get rid of the pagination function and did echo directly in template, which is actually even better in terms of simplification.

echo $results->renderPager(array('arrayToCSV' => false));

However the issue is still there, if I am going to page 3 and then filter results from that page, then again I get correctly the results but they are hidden as they are not numerically enough to have at least 3 pages to show (and so the pagination disappear). The only way to make sure I see filtered results is by submitting the selection only from page 1.

Link to comment
Share on other sites

21 minutes ago, Federico said:

if I am going to page 3 and then filter results from that page

Not sure what you mean by filtering results from that page. You mean you do a new search, right? You have a search form that you include as part of your search results template, and then when you have done a search and are viewing page 3 of the results you do a new search from the search form - is that it?

When you have done that second search, what is the URL in your browser address bar?

Might help too if you post the whole contents of your search template.

Link to comment
Share on other sites

These are the passages that lead to the issue:

__.com/en/works/    this is the page that host all paginated items. So you can go back and forth through pagination and everything works just fine. In this website section, which contain also the search form, I've implemented the search engine that bring search form result values in the same page.

If you go to page 3 or 4 or 5 (for instance), and than you decide to filter items by picking some selects in the search form and submit them (while in page greater than 1), than search results items are retrieved keeping the current page (e.g. page 3 o 4 o 5 depending in which page I was when submitting the form), instead of returning items starting from page 1

So if you submit search form values while in page 3 (__.com/en/works/page3), you might even get results but they will be not visible as they might be not enough to reach page 3 (__.com/en/works/page3?expertise%5B%5D=value01  instead of start over __.com/en/works/?expertise%5B%5D=value01)

Link to comment
Share on other sites

Since the search form has to bring search results in the same page, the form action is simply action="" 

If I insert anything else, the form will redirect to other page (es search.php) which does not serve this case. Other options have been tested but no luck..

Link to comment
Share on other sites

Thank you Macrura,

by inserting the php variable inside the action brackets did the trick. So here's the bottom line, for those that might experience the same issue:

<form name='search' id='yourID' method='get' role='form' action="<?=$pages->get("template=YOURTEMPLATE")->url?>">
.....form values....
</form>

Thank you!

regards

Link to comment
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
 Share

×
×
  • Create New...