Jump to content
diogo

get total pages (pagination)

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.

Share this post


Link to post
Share on other sites

If I understand your need correctly, you can achieve the same result using:

$limit = 5;
$pageslice = $page->children("limit=$limit");
$total = count($pageslice);

Share this post


Link to post
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.

Share this post


Link to post
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 :)

Share this post


Link to post
Share on other sites

Thanks Pete! It's certainly not a big issue, I'm asking more for curiosity. I will use PW's count function as you suggested though :)

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
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 8

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...