# Unexpected result when rendering pages of paginator

**URL:** https://discourse.laminas.dev/t/unexpected-result-when-rendering-pages-of-paginator/2941
**Category:** Components & MVC
**Tags:** laminas-paginator
**Created:** [June 28, 2022, 12:18pm UTC](https://discourse.laminas.dev/t/unexpected-result-when-rendering-pages-of-paginator/2941 "2022-06-28T12:18:05Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![hjerti](https://avatars.discourse-cdn.com/v4/letter/h/a9a28c/32.png) [@hjerti](https://discourse.laminas.dev/u/hjerti)
#### Post date: [June 28, 2022, 12:18pm UTC](https://discourse.laminas.dev/t/unexpected-result-when-rendering-pages-of-paginator/2941/1 "2022-06-28T12:18:05Z")

</div>

Hello you,  
I would like to add the Laminas Paginator to my site. I have a database with a lot of data and I want to get on page 1 only the first 5 database entries, on page 2 only the entries from 6-10 and so on. This works so far. In my test version I have 22 entries. That makes a total of 5 pages. Now I am only shown page 1 with the first 5 entries, but no possibility to click on page 2. I get the maximum entries (22) and divide them by 5, which gives me the total number of pages (5). But since only 5 entries are ever loaded, it also only shows one page with 5 entries.

I would like to set the total page count. I tried that with $paginator-\>setPageRange(ceil($maxPageNumber)); but it doesn’t work. When debugging it comes up with the correct numbers but in the frontend it only shows one page.  
 ![onlyOnePage](https://canada1.discourse-cdn.com/flex032/uploads/zendframework/original/2X/1/1c2048530577ec51d8ec3641917950851d2a526a.png)  
Like you see in the picture it is only one. My wish is, that it shows 5. and when I click on 2, then the next five entries are loaded.

Can someone help me?

---

<div class="post-metadata">

### Author: ![ocramius](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ocramius/32/23_2.png) [@ocramius](https://discourse.laminas.dev/u/ocramius)
#### Post date: [June 28, 2022, 12:35pm UTC](https://discourse.laminas.dev/t/unexpected-result-when-rendering-pages-of-paginator/2941/2 "2022-06-28T12:35:58Z")

</div>

> In my test version

Can you share the automated test+sources where you get to this rendered output, please? I think that’s not how the rendering is supposed to work, so it is likely that you applied a limit on the selection, instead of passing a full paginator to the view renderer.

---

<div class="post-metadata">

### Author: ![hjerti](https://avatars.discourse-cdn.com/v4/letter/h/a9a28c/32.png) [@hjerti](https://discourse.laminas.dev/u/hjerti)
#### Post date: [June 28, 2022, 12:40pm UTC](https://discourse.laminas.dev/t/unexpected-result-when-rendering-pages-of-paginator/2941/3 "2022-06-28T12:40:48Z")

</div>

> [@ocramius](#):
>
> Can you share the automated test+sources where you get to this rendered output, please? I think that’s not how the rendering is supposed to work, so it is likely that you applied a limit on the selection, instead of passing a full paginator to the view renderer.

hey, thanks a lot for the quick answer. this is my handle function in the email-search-handler.  
the “findAllPaginated” gives me only 5 mails. so this could be the issue. I thought I could still output the number of pages based on the total number of entries.

```auto
 /**
     * @param ServerRequestInterface $request
     * @return ResponseInterface
     */
    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        (int)$pageNumber = array_key_exists('page', $request->getQueryParams()) ? $request->getQueryParams()['page'] : 1;

        $firstEmailIdOnPage = $pageNumber === 1 ? 1 : $pageNumber * self::ITEM_COUNT_PER_PAGE;
        $emailsPaginated = $this->emailRepository->findAllPaginated($firstEmailIdOnPage);

        $maxNumOfEmails = $this->emailRepository->countAllEmails();
        (int)$maxPageNumber = $maxNumOfEmails / self::ITEM_COUNT_PER_PAGE;

        $paginator = new Paginator(
            new ArrayAdapter($emailsPaginated)
        );

        $paginator->setCurrentPageNumber((int)$pageNumber);
        $paginator->setPageRange(ceil($maxPageNumber));

        return new HtmlResponse(
            $this->renderer->render('emails::search', [
                'paginator' => $paginator,
                'emails' => $emailsPaginated,
            ])
        );
    }

```

and in the html:

```auto
 <?= $this->paginationControl(
        $this->paginator,
        'sliding',
        'partials::paginator'
    ) ?>

```

---

<div class="post-metadata">

### Author: ![ocramius](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ocramius/32/23_2.png) [@ocramius](https://discourse.laminas.dev/u/ocramius)
#### Post date: [June 28, 2022, 12:58pm UTC](https://discourse.laminas.dev/t/unexpected-result-when-rendering-pages-of-paginator/2941/4 "2022-06-28T12:58:03Z")

</div>

Hey,

> > |   
> > |
> > 
> > - | - |
> 
> ```auto
> 
> $emailsPaginated = $this->emailRepository->findAllPaginated($firstEmailIdOnPage);
> 
> $maxNumOfEmails = $this->emailRepository->countAllEmails();
> 
> (int)$maxPageNumber = $maxNumOfEmails / self::ITEM_COUNT_PER_PAGE;
> 
> $paginator = new Paginator(
> 
> new ArrayAdapter($emailsPaginated)
> 
> );
> 
> ```

The fact that `$emailsPaginated` is an `array` here indicates that your `ArrayAdapter` is probably really only `5` elements big.  
In practice, the paginator is correct in rendering only one page: that’s all you gave to it, and it correctly thinks that there is only one page.

The usual approach to this would be to construct a paginator with **all** the results in it (lazy or eager, depending on backing data size/structure), and then letting the paginator decide which page it is supposed to show.

To try it out, `$emailsPaginated` contained 1000 elements, the rendering would be different: give it a shot.

---

<div class="post-metadata">

### Author: ![hjerti](https://avatars.discourse-cdn.com/v4/letter/h/a9a28c/32.png) [@hjerti](https://discourse.laminas.dev/u/hjerti)
#### Post date: [June 28, 2022, 1:02pm UTC](https://discourse.laminas.dev/t/unexpected-result-when-rendering-pages-of-paginator/2941/5 "2022-06-28T13:02:46Z")

</div>

cool thanks! that sounds clear. the main problem is that in the right setting all emails have huge attachments and this find all method is therefore not “must” be used. so I don’t know if what I want is even possible (total number of pages with simultaneous fetching of only 5 entries)

---

<div class="post-metadata">

### Author: ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)
#### Post date: [June 28, 2022, 1:11pm UTC](https://discourse.laminas.dev/t/unexpected-result-when-rendering-pages-of-paginator/2941/6 "2022-06-28T13:11:56Z")

</div>

Hello and welcome to our forums! 😃

If you use laminas-db, you can use the [`DbSelect` adapter](https://docs.laminas.dev/laminas-paginator-adapter-laminasdb/v1/db-select/) and only the records for the current page will be fetched.

Another option is to use a custom adapter. Example:

```php
class FixedAdapter implements Laminas\Paginator\Adapter\AdapterInterface
{
    private iterable $items;

    private int $count;

    public function __construct(iterable $items = [], int $count = 0)
    {
        $this->items = $items;
        $this->count = $count;
    }

    public function getItems($offset, $itemCountPerPage): iterable
    {
        return $this->items;
    }

    public function count(): int
    {
        return $this->count;
    }
}

```

Usage:

```auto
$paginator = new Laminas\Paginator\Paginator(
    new FixedAdapter($emailsPaginated, $maxNumOfEmails)
);
$paginator->setItemCountPerPage(15);
$paginator->setCurrentPageNumber($pageNumber);

```

* * *

> [@hjerti](#):
>
> `(int)$pageNumber = '…';`

This must be: `$pageNumber = (int)'…';`

---

<div class="post-metadata">

### Author: ![hjerti](https://avatars.discourse-cdn.com/v4/letter/h/a9a28c/32.png) [@hjerti](https://discourse.laminas.dev/u/hjerti)
#### Post date: [June 28, 2022, 1:13pm UTC](https://discourse.laminas.dev/t/unexpected-result-when-rendering-pages-of-paginator/2941/7 "2022-06-28T13:13:37Z")

</div>

thank you @froschdesign ; I will check this out!!

---

<div class="post-metadata">

### Author: ![hjerti](https://avatars.discourse-cdn.com/v4/letter/h/a9a28c/32.png) [@hjerti](https://discourse.laminas.dev/u/hjerti)
#### Post date: [June 29, 2022, 6:04am UTC](https://discourse.laminas.dev/t/unexpected-result-when-rendering-pages-of-paginator/2941/8 "2022-06-29T06:04:03Z")

</div>

thanks you all, it worked perfectly

---

<div class="post-metadata">

### Author: ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)
#### Post date: [June 29, 2022, 8:50am UTC](https://discourse.laminas.dev/t/unexpected-result-when-rendering-pages-of-paginator/2941/9 "2022-06-29T08:50:49Z")

</div>

What do you use now? `DbSelect` or `FixedAdapter`?

---

<div class="post-metadata">

### Author: ![hjerti](https://avatars.discourse-cdn.com/v4/letter/h/a9a28c/32.png) [@hjerti](https://discourse.laminas.dev/u/hjerti)
#### Post date: [July 4, 2022, 10:36am UTC](https://discourse.laminas.dev/t/unexpected-result-when-rendering-pages-of-paginator/2941/10 "2022-07-04T10:36:40Z")

</div>

I use FixedAdapter! This works fine.
