note 102745 added to reserved.variables.request

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
I've implemented John Galt's function as to be able to pass a multi-dimensional array as input parameter. This lets you add, change, or even delete several $_GET's variables at a time.

<?php

function update_gets($parameter) {

    $output = "?";
    $firstRun = true;

    for($index = 0; $index < count($parameter); $index++) {

        foreach($_GET as $key=>$val) {

            if($key != $parameter[$index][0]) {

                if(!isset($_GET[$key]) && $parameter[$index][1] != NULL) {
                    
                    if(!$firstRun) {

                        $output .= "&";

                    }
                    else {

                        $firstRun = false;

                    }

                    $output .= $key."=".urlencode($val);
                    $_GET[$key] = $val;
                }

            }
            else {

                if($parameter[$index][1] != NULL) {

                    if(!$firstRun) {

                        $output .= "&";

                    }
                    else {

                        $firstRun = false;

                    }

                    $output .= $key."=".urlencode($parameter[$index][1]);
                    $_GET[$key] = $parameter[$index][1];

                }
                else {

                    unset($_GET[$key]);

                }

            }

        }

        if(!isset($_GET[$parameter[$index][0]]) && $parameter[$index][1] != NULL) {

            if(!$firstRun) {

                $output .= "&";
            }
            else {

                $firstRun = false;

            }

            $output .= $parameter[$index][0]."=".urlencode($parameter[$index][1]);
            $_GET[$parameter[$index][0]] = $parameter[$index][1];
            
        }

    }

    return htmlentities($output);

}

?>

__________________________

#########################################
#########################################
##                                     ##
##    Structure of $parameter array    ##
##                                     ##
#########################################
#########################################

<?php

      $parameter = array {

            [0] => array {
                  [0] => $key;
                  [1] => $value;
            }

            ...

            [n] => array {
                  [0] => $key;
                  [1] => $value;
            }
      }

?>

#################################################################
#################################################################
##                                                             ##
##    Example Usage                                            ##
##                                                             ##
##    WARNING: THIS FUNCTION WORKS EXACTELY LIKE JOHN          ##
##                   GALT'S ONE, WHICH MEANS YOU CAN USE IT    ##
##                   THE SAME WAY, JUST PASSING AN ARRAY,      ##
##                   STRUCTURED AS SHOWN ABOVE, AS INPUT       ##
##                   ARGUMENT.                                 ##
##                                                             ##
#################################################################
#################################################################

<?php

      #Deleting an existent $_GET's variable, which name is $key:

             $parameter[0][0] = $key;
             $parameter[0][1] = NULL;

             update_gets($parameter);

      #Adding or Changing an existent $_GET's variable, which name is $key:

             $parameter[0][0] = $key;
             $parameter[0][1] = $new_value;

             update_gets($parameter);

?>
----
Server IP: 69.147.83.197
Probable Submitter: 151.20.91.219
----
Manual Page -- http://www.php.net/manual/en/reserved.variables.request.php
Edit        -- https://master.php.net/note/edit/102745
Del: integrated  -- https://master.php.net/note/delete/102745/integrated
Del: useless     -- https://master.php.net/note/delete/102745/useless
Del: bad code    -- https://master.php.net/note/delete/102745/bad+code
Del: spam        -- https://master.php.net/note/delete/102745/spam
Del: non-english -- https://master.php.net/note/delete/102745/non-english
Del: in docs     -- https://master.php.net/note/delete/102745/in+docs
Del: other reasons-- https://master.php.net/note/delete/102745
Reject      -- https://master.php.net/note/reject/102745
Search      -- https://master.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.