Jump to content

get total pages (pagination)


diogo
 Share

Recommended Posts

Is there any built in method that returns the total number of used pages when using pagination?

I did this "by hand" by dividing the total pages by the chosen limit and rounding this number up:

$limit = 5;
$children = $page->children;
$projects = $page->children("limit=$limit");
$total = ceil(count($children) / $limit);

This is working perfectly, but I'm wondering if Ryan already made it simpler for us :)

ps: I know about MarkupPagerNav of course, but I need this for something customized.

Link to comment
Share on other sites

Slkwrm, this will give me the total results on each page. What I want is the number pages resulting from the slicing:

If there are 32 children, and the limit per page is 5, i will have 7 pages of results. 7 is the number I need here.

Link to comment
Share on other sites

$limit = 5;
$projects = $page->children("limit=$limit");
$total = ceil($page->children->count() / $limit);

All I could manage was to make your example slightly shorter, which doesn't answer your question. Mine removes a superfluous $children = $page->children; and uses PW's own count function.

I think that's as short as you'll manage to make it though as I don't know of a function in PW to do exactly what you want. To be honest though it's not really a huge issue (might be nice though) as your code works fine :)

Link to comment
Share on other sites

For any PageArray, count($pageArray) returns the number if items present now, but $pageArray->getTotal() returns the number of items total without pagination. So if you specified a "limit=n" in your selector when loading the pages, you can always call upon that $pages->getTotal() to tell you how many matched in total.

There is also a $pageArray->getStart() method, which tells you the index of the first item. So if you had specified "limit=10" and you were on page2 in the pagination, then getStart() would return 10.

I use these two functions to output a summary of results that tells you were you are. So if you wanted a headline that said this:

Firms with more than 100 staff

Showing firms 1 - 10 of 95

ACME Inc. has 190 staff

Discount Webdev has 155 staff

...

[1] [2] [3] [4] [5] ...

You'd do something like this:

$firms = $pages->find('parent=/firms/, num_staff>100, limit=10');  
$start = $firms->getStart() + 1;
$end = $firms->getStart() + count($firms);
$total = $firms->getTotal();

echo "<h1>Firms with more than 100 staff</h1>";
echo "<h2>Showing firms $start - $end of $total</h2>";

foreach($firms as $firm) {
echo "<p>{$firm->title} has {$firm->num_staff} staff</p>";
}

// output the pagination links
echo $firms->renderPager();

  • Like 5
Link to comment
Share on other sites

Thanks Ryan.

So, we can make Pete's suggestion even a little bit smaller :)

$limit = 5;
$projects = $page->children("limit=$limit");
$total = ceil($projects->getTotal() / $limit);

edit: this is the version I'm using now. And works as expected.

I was asking this because I wanted to have a pagination that is not possible with renderPager.

I don't want the numbers (only previous and next buttons), and I also needed them to have different text depending on the page they are in:

if in the fisrt page: [ next projects ]

if in the middle: [ previous | next projects ]

if in the last page: [ previous projects ]

It's working as I wanted with my handcrafted solution, but as I said, I was wondering if it would be simpler to do it differently.

Link to comment
Share on other sites

  • 5 years later...

Hi folks,

 

New to ProcessWire.  Looking for a solid way to figure out if I'm on the last page of a paginated result.   E.g., there's 10 pages returned, how can I extract 10.

 

I've looked at the documentation, and unless I'm missing something, there isn't a straightforward way to do this.  The above seems like a hack - having to know the result limit seems like a drawback.  Please advise.

 

The only thing I've found that works is something like Math.ceil (TOTAL_RESULT_COUNT / RESULTS_PER_PAGE_LIMIT).  Still, it seems there should be a way to directly grab that last page number.

 

Thanks,

Fred

 

Link to comment
Share on other sites

Hi Fred, here's some code that might be helpful. Also, make sure page numbers are enabled on the page's template (URLs tab in Setup > Templates > any-template). 

$limit = 10; 
$items = $pages->find("template=something, sort=name, limit=$limit");
$total = $items->getTotal();

if($total) {
  // there are results present
  $numPaginations = ceil($total / $limit);
  $pageNum = $input->pageNum();
  if($pageNum == $numPaginations) {
    // you are on the last page of results
  } else if($pageNum == 1) {
    // you are on the first page of results
  } else if($pageNum > $numPaginations) {
    // beyond the last page, do a redirect or a 404
  } else {
    // somewhere in the middle of the paginations
  }  
} else {
  // there were no results on any pagination
}

Also see the PaginatedArray type (PageArray is a PaginatedArray): http://processwire.com/api/ref/paginated-array/

  • Like 9
Link to comment
Share on other sites

  • 2 years later...

After changing a table field to paginated (limit = 30) I can't figure out the right tags to display the total in admin list label.

My current template settings for fields to display in admin lists looked like this:

Termine: {schedule.count}

– but this now displays only the count of page #1. 

Is there an equivalent tag for getTotal() in the field options? I tried {schedule.total}, {schedule.gettotal} … with no success.
Can anybody point me in the right direction?

 

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...