我有 6 条查询记录,此代码在 3 个页面上正确显示:
(action)
public function listAction()
{
$page = (int) $this->params()->fromRoute('id', 0);
$posts = $this->getPagesTable()->selectPages();
$paginator = new Paginator(new PaginatorIterator($posts));
$paginator->setCurrentPageNumber($page)
->setItemCountPerPage(2)
->setPageRange(7);
return new ViewModel(array(
'paginator' => $paginator,
));
}
(view)
<?php foreach ($paginator as $post) : ?>
<h2><?php echo $this->escapeHtml($post->id); ?></h2>
<div><?php echo $this->escapeHtml($post->name);?>
<?php endforeach; ?>
但是当我添加 foreach 循环时:
public function listAction()
{
$page = (int) $this->params()->fromRoute('id', 0);
$posts = $this->getPagesTable()->selectPages();
$paginator = new Paginator(new PaginatorIterator($posts));
$paginator->setCurrentPageNumber($page)
->setItemCountPerPage(2)
->setPageRange(7);
foreach ($paginator as $post) {
// some code or empty foreach
};
return new ViewModel(array(
'paginator' => $paginator,
));
}
分页器不显示最后 (3) 页(第 1 和 2 页正确显示)。 为什么? 谢谢!
如果在foreach {}之后添加$paginator->getIterator()->rewind(),paginator只显示第一页,不显示第二页和第三页。
最佳答案
迭代器模式就像一个指针,在你迭代之后你必须重新设置它。
关于php - ZF2 分页器不显示最后一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15989170/