7

I have 6 records in query and this code correctly show it on 3 pages:

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

but when I add foreach loop in 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);
    foreach ($paginator as $post) {
        // some code or  empty foreach
    };
    return new ViewModel(array(
            'paginator' => $paginator,
            ));
}

paginator don't show last (3) page (1 and 2 page show correctly). Why? Thank you!


If add $paginator->getIterator()->rewind() after foreach {}, paginator show only first page and don't show second and third pages.

morin
  • 171
  • 4
  • 1
    a weird problem, but why do you even have an empty foreach in the Action? If you have removed the code in there to make the example smaller you might have removed the problem! also I would have thought looping through $posts would have been more efficient? – CodeMonkey Apr 13 '13 at 15:09
  • In real project the foreach include some code. – morin Apr 13 '13 at 15:23
  • @CodeMonkey `$posts` contains all rows where `$paginator` have limited items (which are going to display on page). To get those items in `Action` before sending to view, you need such loop (as sampled above as empty loop or `$paginator->getCurrentItems()` or `'$paginator->getItemsByPage($page)` ) which results in empty the `$paginator` items. – webcoder Apr 17 '13 at 03:40
  • @OptimusCrew - so is $this->getPagesTable()->selectPages(); Zend_Db (or zf2 equivalent)? is it pulling down all results from the database to divide them up in php? – CodeMonkey Apr 17 '13 at 10:50
  • @CodeMonkey I've no idea about how zf2 paginator working behind the scenes. May be yes. One thing is for sure, that paginator is buggy and need a lot fixing and logic solutions. – webcoder Apr 18 '13 at 16:04
  • @morin can you confirm how many elements are in posts and how many are in paginator? when you say it doesn't show page 3, so do you mean the page number doesn't appear as a link in the paginator html or when you go to page three's url there is no data? – CodeMonkey Apr 19 '13 at 08:48

2 Answers2

0

The iterator pattern is like a pointer, afeter you iterate you have to reset it.

Magus
  • 2,905
  • 28
  • 36
  • @morin try this: `$paginator->getIterator()->rewind()` – Josias Iquabius Apr 14 '13 at 02:50
  • if add $paginator->getIterator()->rewind() after foreach {}, paginator show only first page and don't show second and third pages. – morin Apr 14 '13 at 04:33
  • I'm not sure but this seems to be a bug. Open an issue at [github](https://github.com/zendframework/zf2/issues), if there's really a bug maybe someone can fix. If this is not a bug someone there could point you what is wrong... – Josias Iquabius Apr 14 '13 at 06:13
0

I also faced same issue

But now i am solved

i am using iterator_to_array($userList);

in view page after removing this line it is working fine may be this is helpful you

Mathan Kumar
  • 929
  • 9
  • 19