[RESOLVED] Pagination does not allow query string parameters

Please don’t refer me to docs, it’s not there. Example from laravel

    public function indexAdmin()
    {
        if ($this->chklog() == 'notlogged') {
            return redirect('admin');
        }
        $page = Request::input('page', '1');
        //$rowsPerPage = 10;
        //$offset = ($page - 1) * $rowsPerPage;
        $dogsearch = (isset($_REQUEST['psch']) <> '' ? $_REQUEST['psch'] : "");
        $dogsch = $dogsearch . "%";
        $aval = (isset($_REQUEST['aval']) <> '' ? $_REQUEST['aval'] : "");
        Session::put('dogsearch', $dogsearch);
        Session::put('dogaval', $aval);
        if ($aval == "n") {
            $dogrows = Dog::where('dogname', 'like', $dogsch)->where('adopted', '=', 1)->count();
        }
        if ($aval == "y") {
            $dogrows = Dog::where('dogname', 'like', $dogsch)->where('adopted', '=', 0)->count();
        }
        if ($aval == "") {
            $dogrows = Dog::where('dogname', 'like', $dogsch)->count();
        }
        Session::put('dogrows', $dogrows);
        $query = Dog::where('dogname', 'like', $dogsch);
        if ($aval == "n") {
            $query->where('adopted', '=', 1);
        } else if ($aval == "y") {
            $query->where('adopted', '=', 0);
        }
        $dogs = $query->orderBy('lastedit', 'DESC')->paginate(5);
        $pagelinks = array('psch' => $dogsearch, 'aval' => $aval);
        $title = 'Admin';
        $view = 'dog/indexadmin';
        $layout = ViewLayout::getLayout('indexadmintp');

        $content = View::make($view)
                ->with('dogs', $dogs)
                ->with('pagelinks', $pagelinks);
        return view($layout)->with('content', $content)
                        ->with('title', $title);
      
    }

notice

 $pagelinks = array('psch' => $dogsearch, 'aval' => $aval);

and in view

echo '<td>' . $dogs->appends($pagelinks)->links() . '</td>';

notice appends

I want something like

http://localhost/laravel55/dog/indexadmin?psch=c&aval=n&page=2

this part

?psch=c&aval=n&page=2

I cannot get a querystring working with pagination in cake 3.* currenty 3.6.

I am no stranger to pagination, I even did a guide in laravel

Please see guide

it shows I know pagination

https://laracasts.com/discuss/channels/guides/length-aware-paginator

I have no problems with Yii2 pagination.

I have never seen anything like the paginator in cake php where you can’t pass querystrings.

I have been through the docs 50 times, there isn’t a way to have the querystring anywhere.

I normally use my own custom paginator with cakephp, but thought I’d try the built in once more.

It doesn’t do anything except give the page=whatever page

Why can’t a good paginator like laravel’s be put into cakephp, so easy to use.

Thankfully I can write my own, but that’s not the point.

Does anyone know how to also use querystring parameters in conjuction with pagination??

I am also number 20 on laravel leaderboard, I am pretty good at laravel.
I state that not as a brag, but to not be toyed with, as I am not new to this stuff.

Every time I fiddle around with cake pagination, I give up in an hour.

Both laravel and yii2 I had the pagination all figured out in 15 minutes.
The custom lengthaware paginator in laravel did take a longer while, I admit.

Also see a previous answer on pagination

Where I use another frameworks paginator in cakephp

with zero trouble.

Another paginator

in controller

        $psch = $this->request->getQuery('psch', '');
        $aval = $this->request->getQuery('aval', '');
        $query = $this->Dogs->find()
            ->where([
                'dogname LIKE' => $psch . '%'
            ])
            ->orderDesc('lastedit');
        if (in_array($aval, ['y', 'n'], true)) {
            $query->where([
                'adopted' => $aval === 'n',
            ]);
        }
        $this->paginate = [
            'limit' => 5
        ];
        $dogs = $this->paginate($query);
        $this->set([
            'dogs' => $dogs
        ]);

in view template

<td><?= $this->Html->link('available chiuauas', [
    '?' => [
        'psch' => 'chiuaua',
        'aval' => 'y'
    ]
]) ?></td>

EDIT
most of paginatior helper functions can take ‘url’ as option::

<?= $this->Paginator->numbers([
    'url' => [
        '?' => [
            'psch' => 'a',
            'aval' => 'n'
        ]
    ]
]) ?>

@Graziel thanks for reply. Now one more thing, how would you cut the number of links down, for example if you had many pages, have only (5 for example) links showing at a time, that just fits mobile better than 10.

Read the docs https://book.cakephp.org/3.0/en/views/helpers/paginator.html#configuring-pagination-options :smiley:

$this->Paginator->options([
    'url' => [
            'psch' => 'a',
            'aval' => 'n'
    ]
]);

And for your next question: https://book.cakephp.org/3.0/en/views/helpers/paginator.html#Cake\View\Helper\PaginatorHelper::numbers

Specifically the modulus option.

From docs:

modulus how many numbers to include on either side of the current page, defaults to 8.

first Whether you want first links generated, set to an integer to define the number of ‘first’ links to generate. Defaults to false. If a string is set a link to the first page will be generated with the value as the title:

echo $this->Paginator->numbers([‘first’ => ‘First page’]);

I do not see an example of modulus.

There should be something like:

  'modulus' => [ /// an example here

No usage.

I know that people who wrote the docs knows cakephp inside and out, but for those of use who did not develop and write the docs, referring me to modulus without a decent example is like telling a 4th grader know you have to work a calculus problem.

But as usual I will try 30 or 40 different ways to use modulus, but an example in the docs would have been better.

I just don’t understand the docs sometimes, instead of giving sound examples, rather you show:

Well it’s kinda like this

but you have to figure the

majority out for yourself

But not complaining, as it is a free opensource framework.

Just weird, There are more videos, search results, tutorials, etc for laravel than cakephp. Yet cakephp has been around so much longer than laravel.

And the link Paginator - 3.10

does not say any thing about appending querystring parameters, it is covering other options only.

Why does the docs only slightly hint at what to do?

It’s as though the docs is meant to be a puzzle.

I don’t like puzzles.

Well I know have

<div class="paginator">
    <ul class="pagination">
            <?= $this->Paginator->first(__('1st')) ?>
            <?= $this->Paginator->prev(__('< ')) ?>
            <?= $this->Paginator->numbers([
                ['modulus' => 2],
               'url' => [
                '?' => [
            'psch' => $psch,
            'aval' => $aval
        ]
    ]
]) ?>
            <?= $this->Paginator->next(__('next') . ' >') ?>
            <?= $this->Paginator->last(__('last') . ' >>') ?>
    </ul>
    <p><?= $this->Paginator->counter(['format' => __('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')]) ?></p>
</div>

modulus did not change anything, still too many links.

If no example, throw me an old bone at least.

Ok changed to this

<div class="paginator">
    <ul class="pagination">
            <?= $this->Paginator->first(__('1st')) ?>
            <?= $this->Paginator->prev(__('< ')) ?>
            <?= $this->Paginator->numbers([
                'modulus' => 2,
               'url' => [
                '?' => [
            'psch' => $psch,
            'aval' => $aval
        ]
    ]
]) ?>
            <?= $this->Paginator->next(__('next') . ' >') ?>
            <?= $this->Paginator->last(__('last') . ' >>') ?>
    </ul>
    <p><?= $this->Paginator->counter(['format' => __('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')]) ?></p>
</div>

At least I have a bone to chew now.

We are well aware that some parts of the documentation are lacking examples, or do not explain things clearly enough. However, as you said, it’s a free open source framework that is developed and maintained by a group of dedicated volunteers. As volunteers we don’t have the time needed to fix every shortcoming of the docs, and so we rely on users such as yourself to edit the documentation when shortcomings are discovered.

Now that you’ve figured out the modulus option, please add a good example into the docs! GitHub - cakephp/docs: CakePHP CookBook

Except that it does :slight_smile: Where it talks about the url option.

You can also append additional URL content into all URLs generated in the helper

It even has an example of it, and to expand on it from your original post and requirements:

$this->Paginator->options([
    'url' => [
        '?' => $pagelinks
    ]
]);

Been busy, but will try to add example later. Meanwhile If I see someone having trouble I will give them the code from this post.

I next want to see how to handle complex queries in the paginator. Currently I would manually calc the offset and just make next and prev links.

I was hoping cakephp had something like making a custom length aware paginator like laravel has.

Either way if I see someone struggling I can show then how to paginate if needed. I will make a post on it later.

what is “length aware paginator” ?

The paginator supports paginating of any query object. So, you can pass it the exact database query that you want. The only requirement is that the passed in object implements the RepositoryInterface, or QueryInterface.

Example from the book:

public function index()
{
   $query = $this->Articles->find('popular')->where(['author_id' => 1]);
   $this->set('articles', $this->paginate($query));
}