Can I pass GET variables while preserving the old GET variables in PHP

Am I using GET for pagination? page = 1 - first page, etc.

The problem is that I would like to sort the data and use GET - the best way to do this:

?page=1&sort=date 

If I try to do this and press page 2, will it go to? page = 2 and will lose sorting.

I also tried $_SERVER['QUERY_STRING'] , but I just finished accumulating ?page=# in the address bar when switching from page to page.

Here's how my page links are generated:

 <a href='{$_SERVER['PHP_SELF']}?page=$nextpage'>Next</a> 

How can I do it?

+4
source share
13 answers

You can do something line by line:

 // Check if there are any GET parameters if (!empty($_GET)) { $link = "?page=2"; // Loop through the parameters foreach ($_GET as $parameter => $value) { // Append the parameter and its value to the new path $link .= "&" . $parameter . "=" . urlencode($value); } } 
+8
source

You can check this topic: How to add GET variables to the end of the current page URL using a form with php?

Basically use $_SERVER['REQUEST_URI'] to get the current url and variables and add get variables to your link.

+3
source

$_GET is an array, so you can iterate over the elements in it to recreate the new URL. There are many examples on a PHP page on $_GET , including one , which shows how to iterate over an array to remove GET parameters from a URL.

+2
source

If you want to add the sort option to your url if it is already set, you can do something like this:

 $qs_sort = isset($_GET['sort']) ? '&sort=' . urlencode($_GET['sort']) : ''; 

Then change your <a> , which you echo to add $qs_sort to the end of the url:

 <a href='{$_SERVER['PHP_SELF']}?page=$nextpage$qs_sort'>Next</a> 
+2
source

You need to decide how many items you show on each page. When this is known, you can easily use the page GET variable to determine which subset of the elements you want to request from the database, for example. if 10 pages are displayed on each page, then the first page is items 0 through 9, page 2 is from 10 to 19, etc. Of course, this requires your database to support some way of selecting a subset of the data.

And of course, if you use a consistent query from your database, it will always be sorted the same way.

+1
source

There are several options:

  • Add a sort option to each of your page links.
  • Save the last sort column in the user session (note that this has the disadvantage that it does not work well when the user opens several tabs)
  • If you use ajax for pagination, add event handlers to page numbers and save javascript the current sort field and send it to the GET request.
+1
source

A few things you could do here ...

  • When adding / changing GET parameters, be sure to add all links to other pages. If you have <a href="?page=1"> Be sure to add &sort=date when the sort function is selected.

  • You can add a sort option with jQuery by clicking on the link -

 var _currentSortMethod = 'date'; $("a.someOtherLink").on('click',function(e){ window.locaton.href = $(this).attr('href') + '&sort=' + _currentSortMethod; e.preventDefault(); }); 
+1
source

In your code, if you control the URL links, you just need to pass this sort variable again to the URL link, for example:

 <?php $sorting = $_REQUEST['sort']; $page = $_REQUEST['page']; $page = $page + 1; echo '<a href="datapage.php?page=' . $page . '&sort=' . $sorting. '>Next Page</a>'; ?> 
+1
source

Just add it back to the URL.

 <?php $sort = (isset($_GET['sort'])) ? "&sort=".$_GET['sort'] : ""; ?> <a href="link.php?page=2<?php echo $sort; ?>">link</a> 
+1
source

When retrieving sorting links, find the current page and write down the link containing this page. Also, when paginator links are displayed, they are written with the detected sort.

Let's say you are on page 2 and sort the list by name in descending order, your links should look like this:

  • sort by name
 <a href="?page=2&amp;sort=title+desc>Title</a> 
  • sorting by order
 <a href="?page=2&amp;sort=order+desc>Order</a> 
  • paginated page
 <a href="?page=1&amp;sort=title+desc>1</a> 
  • page 3
 <a href="?page=3&amp;sort=title+desc>3</a> 

etc.

+1
source

You can try the following:

 $query = new HttpQueryString(); $query->set("page", $nextPage); echo "http://www.example.com/?" . $query->toString(); 

The following is additional documentation on the HttpQueryString class: http://www.php.net/manual/en/class.httpquerystring.php

Hope this helps!

+1
source

Large-scale pagination is usually done using the database APIs. Small-scale pagination can be performed using PHP sessions using the $ _SESSION variable.

0
source

For those who want a solution that supports the current parameters, updates if a new value is set, and adds new parameters, this performs the task:

The code

 $domainurl='https://' . $_SERVER['SERVER_NAME']; $currenturl=$domainurl . $_SERVER['PHP_SELF']; function urlVars($new=array()) { $url=$GLOBALS['currenturl']; $vars=array_merge($_GET, $new); foreach ($vars as $key => $var) { $params[$x]='&' . $key . '=' . $var; $x++; } $str='?' . trim(implode($params), '&'); return $url .= $str; } 

Usage example:

  <a href="' . urlVars(array('page'=>'2', 'view'=>'30', etc.)) . '">Go To Page 2 & Show 30 Results</a> 

Using the variable $currenturl , which returns only the domain and path to the current page, not $ _SERVER ['REQUEST_URI'], which returns a query string, also solves the problem of repeated queries and allows you to change previously set parameters.

This is because array_merge() will hold two or more arrays together, and if there are several keys with the same name, the last one declared takes the cake, and all the rest will be canceled. And since the $_GET array is filled with the old parameters, it must arrive before the $new array. Example:

 $_GET['page']='1'; if ($new['page']='1') {array_merge($_GET, $new);} // 'page'=>'1' elseif ($new['page']='4') {array_merge($_GET, $new);} // 'page'=>'4' $_GET['page']='4'; if ($new['page']='2') {array_merge($_GET, $new);} // 'page'=>'2' 

Note

The elegant function of this function and the whole reason I worked with it for several hours until it was perfect is that you do not need to specify the same parameters every time you have a new link / form or have some something is a shutdown array in another script that contains possible parameter keys. All that you put in the $new array are parameters that are new (obviously) or are changing. It is universal!

-1
source

Source: https://habr.com/ru/post/1416671/


All Articles