keeping url parameters during pagination
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.
Django Pagination Keep Current URL Parameters
Here is one way to fix it.
views.py
def your_view(request):
....
query = request.GET.get('q') # Query or None
...
context = {'query': query}
Add {% if query %}&q={{ query }}{% endif %} in your a tag
templates
<a href="?page={{ page.previous_page_number }}{% if query %}&q={{ query }}{% endif %}">
<i class="previous fa fa-arrow-left fa-lg"></i>
</a>
How to keep query parameters during pagination with webhelpers.paginate
After experimenting on the shell for a while I think I found a solution.
There's a kwargs dictionary defined in currentPage (after being assigned from paginate.Page), so I made some experiments sending parameters and it worked. This is how.
currentPage = paginate.Page(products, page, items_per_page=50)
currentPage.kwargs['q'] = q
return dict(currentPage=currentPage,
title=u'Products List', item=u'product', items=u'products',
data=currentPage.items,
grid=product_grid,
page=u'Search %s results' % q,
q=q,
hits=len(products))
now I get this kind of links: '/list?q=cable&page=2' still wondering if it is the best solution or the best practice
Pagination + URL Params in Laravel 7
Your solution works but there is a built-in method for achieving your desired result in Laravel 7.
{{ $visitors->withQueryString()->links() }}
You can see your exact problem being solved in the attached PR: https://github.com/laravel/framework/pull/31648
FYI, links()
replaced render()
in Laravel 5.3
pass parameters to pagination url
Yo can use this function:
http_build_query
php.net example:
<?php
$data = array('foo'=>'bar',
'baz'=>'boom',
'cow'=>'milk',
'php'=>'hypertext processor');
echo http_build_query($data) . "\n";
echo http_build_query($data, '', '&');
?>
Output:
foo=bar&baz=boom&cow=milk&php=hypertext+processor
foo=bar&baz=boom&cow=milk&php=hypertext+processor
Keep Search Parameters Through Pagination
Append the search data to the pagination links()
<?php echo $employees->appends(array("srch_lname" => ...))->links(); ?>
http://laravel.com/docs/4.2/pagination#appending-to-pagination-links
PHP Laravel - keep using the passed paramaters in the url for other urls like pagination links
try this => $paginator->appends(request()->query->all())->previousPageUrl();
Related Topics
Error: File Is Encrypted or Is Not a Database
Updating Product Stock Programmatically in Woocommerce 3
PHP - Returning the Last Line in a File
How to Correct the [Composer\Downloader\Transportexception] Error for Composer
Using Simplexml to Load Remote Url
Php: iPad Does Not Play Mp4 Videos Delivered by PHP, But If Accessed Directly It Does
How to Add Namespace to an Attribute with PHP's Simplexml
Fix Incorrectly Displayed Encoding on an HTML Document with PHP
How to Get User Timezone Using Jquery
Use Git Smudge/Clean to Replace File Contents
How to Make a Cascading Drop Down List in PHP Using Jquery
How to Download Large Files Through PHP Script