Jump to content

Max Number of Pages to paginate


Orkun
 Share

Recommended Posts

Hi Guys

This is possibly a stupid question but I can't get it to work. How can I limit the max number of pages I get and paginate them afterwards with a different limit(per pagination page)?

Lets say I have a total of 1000 Events. I want to output 100 Events (10 Events per pagination page).

// outputs 10 events per page but returns all 1000 events - how pagination works default
$items = $pages->find("template=event, limit=10");

// should output 10 events per page but should only use the 100 latest events for output - doesn't work 
// Try 1
$items = $pages->find("template=event, limit=100")->find("limit=10");
// Try 2
$items = $pages->find("template=event, limit=10, getTotal=100");

// output pager
echo $items->renderPager();

Can someone clarify that to me?

Greetings Orkun

Link to comment
Share on other sites

Thanks @flydev - but this doesn't answer my question.

I only need 100 entries from the database, even though there might be more.
So let's say, I have 200 entries in the DB, I only want the first 100 and THEN paginate these 100 entries.

When I use limit/start, though, I always get all entries with the find-method.

Link to comment
Share on other sites

10 minutes ago, Martijn Geerts said:

Take a look at @LostKobrakai's post: 

But don't set a new Page array, but use the pagearray you want to use.

 

4 minutes ago, flydev said:

try :


$items = $pages->find("template=event")->setTotal(100)->setLimit(10);

 

Thanks both of you.

But now the pager doesn't work at all, it just outputs all events.

My code looks like this:

$items = $pages->find("sort=-created, template={$page->choosetemplate->title}, choose_sender_2016_multi|parent.choose_sender_2016={$page->rootParent->choose_sender_2016}")->setTotal(10)->setLimit(5);

$pager = $this->modules->get('MarkupPagerNav');
$paginationMarkup = $pager->render($items);

 

Link to comment
Share on other sites

12 minutes ago, Robin S said:

How about:


$items = $pages->find("template=event, limit=10")->setTotal(100);
echo $items->renderPager();

 

Thanks @Robin S

Now the limit per page is working, but the total is ignored - still getting all records from the database.

[Edit] it seems to be working. But only if the number in the total is dividable by the limit without a rest.
So limit = 3 and setTotal = 10 doesn't work, but limit = 3 and setTotal = 9 works

Edited by Nukro
Added solution
Link to comment
Share on other sites

18 minutes ago, Nukro said:

[Edit] it seems to be working. But only if the number in the total is dividable by the limit without a rest.
So limit = 3 and setTotal = 10 doesn't work, but limit = 3 and setTotal = 9 works

I think setTotal() is something that only applies to the pagination calculation. It doesn't place a hard limit on the results from the find() - that has as many items as there are matching results. So the results still have 1000 items or whatever, and setTotal() just tells the pager to stop at 100. Easy solution is to make your total evenly divisible by your items-per-page limit.

Link to comment
Share on other sites

There's a typo in the example I've given, it's not $this->modules, but $modules in template context.

$items = $pages->find("template=event, limit=100");
$items->setTotal($items->count())->setLimit(10)->setStart(0);
$pager = $modules->get('MarkupPagerNav');
$pagination = $pager->render($items);

echo $pagination;

 

Link to comment
Share on other sites

Thanks for your input guys, @Robin S & @Martijn Geerts

Lets clarify a few points what I have tried already.

# Method 1

// The customer wants to set the items per page. Lets say it's 10 for the moment
$limit = $page->number;

// Remember: There are only 12 Event Pages at the moment
$items = $pages->find("sort=-created, template={$page->choosetemplate->title}, choose_sender_2016_multi|parent.choose_sender_2016={$page->rootParent->choose_sender_2016}, limit=$limit")->setTotal(100);

// Output Markup
$paginationMarkup = $items->renderPager(array(
	'nextItemLabel' => '<i class="fa fa-angle-right"></i>', 
	'previousItemLabel' => '<i class="fa fa-angle-left"></i>', 
));

This method works halfway. It only works when the setTotal Number is divisible with the limit. But another thing that I noticed is, that now it outputs 10 pagination Links. The first pagination page renders 10 events, the second page renders 2 events and all the other pagination pages are empty. (visualized in the image)

pager.png

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

# Method 2 (Martijns Method)

// Remember: There are only 12 Event Pages at the moment
$items = $pages->find("sort=-created, template={$page->choosetemplate->title}, choose_sender_2016_multi|parent.choose_sender_2016={$page->rootParent->choose_sender_2016}, limit=100");

// Remember $limit is 10 at the moment (set by user in the backend by an integer field)
$items->setTotal($items->count())->setLimit($limit)->setStart(0);

$pager = $modules->get('MarkupPagerNav');

$paginationMarkup = $pager->render($items, array(
	'nextItemLabel' => '<i class="fa fa-angle-right"></i>', 
	'previousItemLabel' => '<i class="fa fa-angle-left"></i>', 
));

This example doesn't really work. It outputs all 12 events on the first pagination page and on the second page the pager disappears and no events are displayed.

pager2.png

 

Greetings Orkun

Link to comment
Share on other sites

You need to keep in mind, that you're in charge of any logic regarding the total of pages if you overwrite it, so you need to handle cases, where the total is less than 100 as well.

// The customer wants to set the items per page. Lets say it's 10 for the moment
$limit = $page->number;

// Remember: There are only 12 Event Pages at the moment
$items = $pages->find("some=selector, limit=$limit");
$items->setTotal(min($items->getTotal(), 100));

// Output Markup
$paginationMarkup = $items->renderPager(array(
	'nextItemLabel' => '<i class="fa fa-angle-right"></i>', 
	'previousItemLabel' => '<i class="fa fa-angle-left"></i>', 
));

 

  • Like 3
Link to comment
Share on other sites

29 minutes ago, LostKobrakai said:

You need to keep in mind, that you're in charge of any logic regarding the total of pages if you overwrite it, so you need to handle cases, where the total is less than 100 as well.


// The customer wants to set the items per page. Lets say it's 10 for the moment
$limit = $page->number;

// Remember: There are only 12 Event Pages at the moment
$items = $pages->find("some=selector, limit=$limit");
$items->setTotal(min($items->getTotal(), 100));

// Output Markup
$paginationMarkup = $items->renderPager(array(
	'nextItemLabel' => '<i class="fa fa-angle-right"></i>', 
	'previousItemLabel' => '<i class="fa fa-angle-left"></i>', 
));

 

Yeah you're right. Because of this I had done this:

$items = $pages->find("sort=-created, template={$page->choosetemplate->title}, choose_sender_2016_multi|parent.choose_sender_2016={$page->rootParent->choose_sender_2016}, limit=$page->number");
if($items->getTotal() >= 100){
	$items = $items->setTotal(100);
}
$paginationMarkup = $items->renderPager(array(
	'nextItemLabel' => '<i class="fa fa-angle-right"></i>', 
	'previousItemLabel' => '<i class="fa fa-angle-left"></i>', 
));

But now I see that your solution is much cleaner :), Thanks @LostKobrakai

Link to comment
Share on other sites

@Nukro, did you solve the issue of the number of items on the last page of pagination?

For example, if you make setTotal(50) and limit=7 and your find() actually finds more than 50 pages, then the second to last page will have results 43-49, meaning you want the last page to have only one result. But it will instead show 7 results giving you 56 results in total instead of the 50 you want.

I played around and this seems to solve that:

$limit = 7; // could be set from a field in your page
$items = $pages->find("has_parent=1, limit=$limit"); // whatever selector, this was just for testing
$total = min($items->getTotal(), 50);
$items->setTotal($total);
$remainder = $total - $items->getStart();
if($remainder < $limit) {
    $items->removeItems(range($remainder, $limit))->setTotal($total);
}
$start = $items->getStart() + 1;
$end = $items->getStart() + count($items);

echo "<p>Showing items $start - $end of $total</p>";
echo $items->each("<p>{title}</p>");
echo $items->renderPager();

 

  • Like 4
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...