7

I have a zend paginator object, I want to get the first element in this paginator.

I tried $paginator->getItem(0) but it returns the message:Message: Cannot seek to 0 which is below the offset 2. And the $paginator->count() is 19.

I can achieve this by using foreach:

foreach ($paginator as $item)
{
    $entry = $item;
}

How can I get this by not using foreach?

shanethehat
  • 15,460
  • 11
  • 57
  • 87
hungneox
  • 9,333
  • 12
  • 49
  • 66

3 Answers3

6

This will give you the first item without using foreach:

$first = current($paginator->getItemsByPage(1)); // Get the first item
$firstCurrent = current($paginator->getCurrentItems()); // Get the first item of the current pages
Ilians
  • 743
  • 7
  • 23
  • excuse me, but I sill don't know how to use the first item by that way? – hungneox Jan 12 '12 at 01:50
  • The `$first` and `$firstCurrent` should have the same value as the `$item` would have in the foreach. If the `$entry` variable from your foreach is the value you're trying to get, the snippet I posted should also do the trick. – Ilians Jan 12 '12 at 09:55
  • Could you provide a var_dump of the `$paginator->getItemsByPage(1)` and `$paginator->getCurrentItems()`? – Ilians Jan 12 '12 at 12:01
  • current() returns only true or false, I can use it as an item in paginator. – hungneox Jan 13 '12 at 03:50
  • It returning `false` means the provided array is empty. Another approach (which should give the same result) would be `$paginator->getCurrentItems()->current()`. But since the result appears to be empty the paginator doesn't seem to return any current items. I'm not too sure why it won't work like this but it would using foreach, but if you could provide the paginator creating / adding the data code it'd be helpful. – Ilians Jan 13 '12 at 11:07
1

It should be

$paginator->getCurrentItems()->current();
rukavina
  • 184
  • 1
  • 6
0

This will count number of sub pages in rowset:

$paginator->count();

This will count the total number of items in rowset:

$paginator->getTotalItemCount();

If you have more than 1 sub page, maybe you need to use second parameter in getItem(), which is a number of sub page?

$paginator->getItem(1, 1);

BTW: getItem() is not zero based, so first element in rowset is getItem(1).

In my similar situation I have 1 sub page, and using $paginator->getItem(1) give me the right result

sanneo
  • 365
  • 4
  • 15