Applying pagination to results from a database

Hi

I’m trying to apply pagination to results from a database. I’ve managed to connect to the database, display the results, and limit the number of items displayed. However when it comes to displaying the page links, I get the following error ‘Call to undefined method Kirby\Toolkit\Pagination::nextPageURL()’.

This is my controller

return function($kirby, $site, $pages, $page) {

        $shared = $kirby->controller('site' , compact('page', 'pages', 'site', 'kirby'));
        
        $poruke = Db::select('smsporuke', ['Drzava', 'Datum', 'Broj', 'Poruka'], null, 'Datum DESC', 0, 30);

        // apply pagination
        $poruke     = $poruke->paginate(20);
        $pagination = $poruke->pagination();
      
        return A::merge($shared , compact('poruke', 'pagination'));

and this is my template


            <?php foreach ($poruke as $poruka): ?>
                <p class='mb-2'><strong>Drzava:</strong> <?= $poruka->Drzava() ?> - <strong>Datum:</strong> <?= $poruka->Datum() ?> <strong>Broj:</strong> <?= $poruka->Broj() ?> - <strong>Poruka:</strong> <?= $poruka->Poruka() ?></p>
            <?php endforeach ?>

            <?php if($poruke->pagination()->hasPages()): ?>
            <nav class="pagination">

            <?php if($poruke->pagination()->hasNextPage()): ?>
            <a class="next" href="<?php echo $poruke->pagination()->nextPageURL() ?>">&lsaquo; older posts</a>
            <?php endif ?>

            <?php if($poruke->pagination()->hasPrevPage()): ?>
            <a class="prev" href="<?php echo $poruke->pagination()->prevPageURL() ?>">newer posts &rsaquo;</a>
            <?php endif ?>

            </nav>
            <?php endif ?>

Can anyone spot the mistake? I’m using the same pagination code on my blog, and it works fine.

The method names spelling is wrong, must be nextPageUrl() and prevPageUrl().

Also, since your controller already has the $pagination variable defined, why don’t you use it, instead of $poruke->pagination()?

Hi Sonja

Is that in the a href, url part or the if statement?

Re the $pagination variable, I still have to update that part of the code…

What?

Ignore that, I’ve updated the code

            <?php if($pagination->hasNextPage()): ?>
            <a class="next" href="<?= $pagination->nextPageUrl() ?>">&lsaquo; older posts</a>
            <?php endif ?>

            <?php if($pagination->hasPrevPage()): ?>
            <a class="prev" href="<?= $pagination->prevPageUrl() ?>">newer posts &rsaquo;</a>
            <?php endif ?>

Still getting the same error…

What do you get if you

dump($poruke);

And what if you

dump($pagination);

dump poruke

Kirby\Toolkit\Collection Object
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
    [6] => 6
    [7] => 7
    [8] => 8
    [9] => 9
    [10] => 10
    [11] => 11
    [12] => 12
    [13] => 13
    [14] => 14
)
Kirby\Toolkit\Collection Object
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
    [6] => 6
    [7] => 7
    [8] => 8
    [9] => 9
    [10] => 10
    [11] => 11
    [12] => 12
    [13] => 13
    [14] => 14
)

dump pagination

Kirby\Toolkit\Pagination Object
(
    [page:protected] => 1
    [total:protected] => 30
    [limit:protected] => 15
    [propertyData:protected] => Array
        (
            [page] => 
            [total] => 30
            [limit] => 15
        )

)
Kirby\Toolkit\Pagination Object
(
    [page:protected] => 1
    [total:protected] => 30
    [limit:protected] => 15
    [propertyData:protected] => Array
        (
            [page] => 
            [total] => 30
            [limit] => 15
        )

)

Not sure why they both dump a double?!

Ok, the reason the methods don’t work is that we are not dealing with a pages collection but a
Kirby\Toolkit\Collection and a Kirby\Toolkit\Pagination object which doesn’t have nextPageUrl()/prevPageUrl() methods


https://getkirby.com/docs/reference/objects/toolkit/pagination

So does that mean it’s not possible to do it?