How to work with $_SERVER['QUERY_STRING']

后端 未结 3 447
不知归路
不知归路 2020-12-28 09:35

How to work with $_SERVER[\'QUERY_STRING\'] and pagination?

When my table is sorted by this link:



        
相关标签:
3条回答
  • 2020-12-28 09:59

    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.

    0 讨论(0)
  • 2020-12-28 10:08

    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 )

    0 讨论(0)
  • 2020-12-28 10:12

    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> ';
    
    0 讨论(0)
提交回复
热议问题