Jump to content

How to: pagination with different limit for first page


Raymond Geerts
 Share

Recommended Posts

I was wondering how to achieve the following.

A total of 100 pages which are listed with a limit of 10 items per page, resulting in 10 pages of 10 items.

Instead i desire the following (below), and wondering how to setup the code / selector(s).

The first page should show a list of 8 items. After that all following pages should have a limit of 10 items.

  • page 1 -> items 1 - 8 -> (limit 8)
  • page 2 -> items 9 - 18 -> (limit 10)
  • page 3 -> items 19 - 28 -> (limit 10)
  • ...
  • page 9 -> items 79 - 88 -> (limit 10)
  • page 10 -> items 89 - 98 -> (limit 10)
  • page 11 -> items 98 - 100 -> (limit 10)

The ideal situation would be to have an extra page selector property aside of 'limit', something named 'limit_first' or alike.

Link to comment
Share on other sites

Quote

Maybe you can adapt this to work as you need it: 

This example works by adding (prepending) an item to the list. What i'm looking for is a way to manipulate the `start` value and wonder if the following might do the trick.
 

<?php

$limit = 10;
$limit_first = 8;
$offset = $limit_first - $limit;

if ($input->pageNum == 1){
    $products = $pages->find("template=product, sort=-date_pub, start=0, limit=$limit_first");
}
else {
    $start = (($input->pageNum - 1) * $limit) + $offset;
    $products = $pages->find("template=product, sort=-date_pub, start=$start, limit=$limit");
}


 

  • Like 1
Link to comment
Share on other sites

1 hour ago, Raymond Geerts said:

What i'm looking for is a way to manipulate the `start` value and wonder if the following might do the trick.

Sounds like a good solution, but renderPager() will show a different number of pages on the first page than the others (because it is dividing 100 by 8 instead of 10). So you may need to use a custom pager to get around this.

  • Like 1
Link to comment
Share on other sites

I have been playing around with the code from @Raymond Geerts, getting the pagination working. Thought I would post this for posterity:

$limit = 10;
$limit_first = 8;
$offset = $limit - $limit_first;

if ($input->pageNum == 1){
    $results = $pages->find("limit=$limit_first");
}
else {
    $start = (($input->pageNum - 1) * $limit) - $offset;
    $results = $pages->find("start=$start, limit=$limit");
}

// output result items
echo $results->each("<p>{name}</p>");

// override pagination
$results->setLimit($limit)->setTotal($results->getTotal() + $offset)->setStart((($input->pageNum - 1) * $limit));
// output pager
echo $results->renderPager();

 

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