我有 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(),分页器只显示第一页,不显示第二页和第三页。