Return to Snippet

Revision: 16397
at August 3, 2009 01:19 by traeregan


Updated Code
//
// Example: echo '<a href="?page='. $previousPage . rebuildQueryString("page,submit") .'">Prev</a>';
// This would carry over any querystrings from the current URL, minus the current
// $_GET['page'] and $_GET['submit'] keys and values if they exist
//
function rebuildQueryString($curr_vars='')
{
    if(!empty($_SERVER['QUERY_STRING']))
    {
        $parts = explode("&", $_SERVER['QUERY_STRING']);
        $curr_vars = str_replace(" ", "", $curr_vars);
        $c_vars = explode(",", $curr_vars);
        $newParts = array();
        
        foreach ($parts as $val)
        {
            $val_parts = explode("=", $val);
            if(!in_array($val_parts[0], $c_vars))
            {
            	array_push($newParts, $val);
            }
        }
        
        if(count($newParts) != 0)
        {
            $qs = "&".implode("&", $newParts);
        }
        else
        {
            return false;
        }
        return $qs;
    }
    else
    {
        return false;
    }
}

Revision: 16396
at August 3, 2009 00:32 by traeregan


Initial Code
//
// Example: <a href="?page='. $previousPage . rebuildQueryString("page") .'">Prev</a>
// This would carry over any querystrings from the current URL, minus the page key and value
//
function rebuildQueryString($curr_vars='')
{
    if(!empty($_SERVER['QUERY_STRING']))
    {
        $parts = explode("&", $_SERVER['QUERY_STRING']);
        $curr_vars = str_replace(" ", "", $curr_vars);
        $c_vars = explode(",", $curr_vars);
        $newParts = array();
        
        foreach ($parts as $val)
        {
            $val_parts = explode("=", $val);
            if(!in_array($val_parts[0], $c_vars))
            {
            	array_push($newParts, $val);
            }
        }
        
        if(count($newParts) != 0)
        {
            $qs = "&".implode("&", $newParts);
        }
        else
        {
            return false;
        }
        return $qs; // this is your new created query string
    }
    else
    {
        return false;
    }
}

Initial URL
http://www.finalwebsites.com/snippets.php?id=6

Initial Description
Credit: FinalWebsites.com
The only argument is a comma separated list of querystring vars that you don't want to carry over.  At minimum you should pass the name of the var that you're appending the existing querystring(s) to so it's not included twice in the URL.

Initial Title
Rebuild Querystring

Initial Tags


Initial Language
PHP