Instead of reusing QUERY_STRING
, you should assemble it anew with http_build_query().
// Merge $_GET with new parameter
$QS = http_build_query(array_merge($_GET, array("page"=>2)));
// You should apply htmlspecialchars() on the path prior outputting:
echo "<a href='" . htmlspecialchars("$_SERVER[PHP_SELF]?$QS") . "'> $i </a>";
Thus you have all current $_GET
parameters included, but can add or replace entries with new values. And it's ensured that each appears only once.
Create a link builder ( simple php whitch make array( "a" => "b", "c" => "d" ) into ?a=b&c=d and rebuild it each time ( eg. put there "sort_name" => ... , "sort" => ..., "page" => ... )
If you still want to use QUERY_STRING - check if it contains &page=... and replace it ( both made by regexp )
Don't use QueryString, just create a variable at the beginning of your script:
$pagerUrl = $_SERVER['PHP_SELF'].'?sort_name='.$_GET['sort_name']&sort=$_GET['sort'];
And use it in your link:
echo '<a href="'.$pagerUrl.'&page='.$i.'">'.$i.'</a> ';