keeping url parameters during pagination

后端 未结 4 1795
南方客
南方客 2020-11-30 14:03

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_i         


        
4条回答
  •  忘掉有多难
    2020-11-30 14:41

    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.

提交回复
热议问题