2012-01-11 90 views
6

我有一個zend paginator對象,我想獲得這個paginator中的第一個元素。我試過$paginator->getItem(0),但它返回信息:Message: Cannot seek to 0 which is below the offset 2。和$ paginator->計數()是19Zend Paginator - 如何獲取paginator中的第一個元素?

我可以使用的foreach實現這一點:

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

我怎麼能不使用的foreach得到這個?

回答

5

這會給你的第一個項目,而無需使用的foreach:

$paginator->count(); 

這將計數行集中的項目總數:

$first = current($paginator->getItemsByPage(1)); // Get the first item 
$firstCurrent = current($paginator->getCurrentItems()); // Get the first item of the current pages 
+0

原諒我,但我門坎不知道如何使用第一這樣的項目? – hungneox 2012-01-12 01:50:32

+0

'$ first'和'$ firstCurrent'應該和'$ item'在foreach中的值相同。如果你的foreach中的'$ entry'變量是你試圖獲得的值,我發佈的代碼段也應該這樣做。 – Ilians 2012-01-12 09:55:57

+0

dind't工作夥計:( – hungneox 2012-01-12 10:51:32

0

這將行集數子頁面數:

$paginator->getTotalItemCount(); 

如果您有超過1個子頁面,可能需要使用0123中的第二個參數,這是多少個子頁面?

$paginator->getItem(1, 1); 

順便說一句:getItem()不是基於零,在這樣的行集第一元件是getItem(1)

在我類似的情況我有1個頁面,並使用$paginator->getItem(1)給我正確的結果

0

應該

$paginator->getCurrentItems()->current();