Asked  7 Months ago    Answers:  5   Viewed   39 times

Is there any way to keep my GET parameters when paginating.

My problem is that I have a few different urls i.e

questions.php?sort=votes&author_id=1&page=3

index.php?sort=answers&style=question&page=4

How in my pagination class am I supposed to create a link to the page with a different page number on it but yet still keep the other parts of the url?

 Answers

72

In short, you just parse the URL and then you add the parameter at the end or replace it if it already exists.

$parts = parse_url($url) + array('query' => array());
parse_str($parts['query'], $query);
$query['page'] = $page;
$parts['query'] = http_build_str($query);
$newUrl = http_build_url($parts);

This example code requires the PHP HTTP module for http_build_url and http_build_str. The later can be replaced with http_build_query and for the first one a PHP userspace implementation exists in case you don't have the module installed.

Another alternative is to use the Net_URL2 package which offers an interface to diverse URL operations:

$op = new Net_URL2($url);
$op->setQueryVariable('page', $page);
$newUrl = (string) $op;

It's more flexible and expressive.

Wednesday, March 31, 2021
 
buymypies
answered 7 Months ago
16

Well, it's been a month and a few days. I also encountered your problem, I just got my solution for it.

What I did was, I called the Paginator class then used a static method to force the pagination to whatever page I am in.

Paginator::currentPageResolver(function() use ($current_page) {
    return $current_page;
});

Then call the data that you wanted to be paginated. Here's my whole code below.

<?php

use IlluminatePaginationPaginator;

$app->get('/controller/clients', $controllerAuthenticated(), function() use ($app) {

    $request = $app->request;
    $current_page = $request->get('page');

    Paginator::currentPageResolver(function() use ($current_page) {
        return $current_page;
    });

    $clients = $app->client->where(function($query) use ($client_name, $country_id) {
        if ($client_name != '') {
            $query->where('client_name', 'LIKE', '%'. $client_name .'%');
        }

        if ($country_id != '') {
            $query->where('country_id', $country_id);
        }
    })
    ->orderBy('client_name', 'ASC')
    ->paginate(1);

    $clients->setPath($app->urlFor('controller.client.all'));

    $app->render('/controller/client/all.twig', [
        'clients'   => $clients,
        'countries' => $countries,
        'request'   => $request
    ]);
})->name('controller.client.all');

I forgot where I saw the answer though, I'll post a link to a reference once I found it.

Wednesday, March 31, 2021
 
Easen
answered 7 Months ago
93

Read this and find the "Customizing the "Current Page" Link" section. Use that to add custom markup then add appropriate CSS.

Edit: the OP found the following solution:

$pagination['uri_segment'] = 2;
Saturday, May 29, 2021
 
Bere
answered 5 Months ago
54

If I understand your problem correctly, then you need a rewriterule something like this:

RewriteRule ([a-zA-Z_-]+)/([0-9]?) index.php?id=$1&page=$2

Which then directs /mycoolpage/2 to index.php?id=mycoolpage&page=2 etc

And if the last number is not present, then the page query parameter will simply be blank (so you'd assume page=1)

Saturday, May 29, 2021
 
nasty
answered 5 Months ago
16

fascinating, this is the first time I've encounter them, found this
http://doriantaylor.com/policy/http-url-path-parameter-syntax I also found this http://tools.ietf.org/html/rfc3986#section-3.3 (last paragraph before query) and this http://www.jtmelton.com/2011/02/02/beware-the-http-path-parameter/

their rarely used, I think their meant to attach certain properties to paths .. maybe even control which version of segment you want to use, but this is just a hunch ... either way thank you, for bringing it up.

Wednesday, July 28, 2021
 
Axalix
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :