Jump to content


Photo

get total pages (pagination)


  • Please log in to reply
6 replies to this topic

#1 diogo

diogo

    Hero Member

  • Moderators
  • 2,068 posts
  • 1179

  • LocationPorto, Portugal

Posted 18 March 2012 - 07:46 PM

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.

#2 slkwrm

slkwrm

    Sr. Member

  • Members
  • PipPipPipPip
  • 279 posts
  • 101

Posted 19 March 2012 - 04:04 AM

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

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


#3 diogo

diogo

    Hero Member

  • Moderators
  • 2,068 posts
  • 1179

  • LocationPorto, Portugal

Posted 19 March 2012 - 05:44 AM

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.

#4 Pete

Pete

    Administrator

  • Administrators
  • 1,802 posts
  • 727

  • LocationChester, England

Posted 19 March 2012 - 07:05 AM

$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 :)

#5 diogo

diogo

    Hero Member

  • Moderators
  • 2,068 posts
  • 1179

  • LocationPorto, Portugal

Posted 19 March 2012 - 07:21 AM

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 :)

#6 ryan

ryan

    Hero Member

  • Administrators
  • 5,985 posts
  • 3386

  • LocationAtlanta, GA

Posted 19 March 2012 - 08:30 AM

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();



#7 diogo

diogo

    Hero Member

  • Moderators
  • 2,068 posts
  • 1179

  • LocationPorto, Portugal

Posted 19 March 2012 - 11:01 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users