keeping url parameters during pagination

If you wanted to write your own function that did something like http_build_query, or if you needed to customize it’s operations for some reason or another:

<?php 
function add_edit_gets($parameter, $value) { 
    $params = array(); 
    $output = "?"; 
    $firstRun = true; 
    foreach($_GET as $key=>$val) { 
        if($key != $parameter) { 
            if(!$firstRun) { 
                $output .= "&"; 
            } else { 
                $firstRun = false; 
            } 
            $output .= $key."=".urlencode($val); 
         } 
    } 

    if(!$firstRun) 
        $output .= "&"; 
    $output .= $parameter."=".urlencode($value); 
    return htmlentities($output); 
} 

?>

Then you could just write out your links like:

<a href="https://stackoverflow.com/questions/9666909/<?php echo add_edit_gets("page", "2"); ?>">Click to go to page 2</a>

Leave a Comment