note 95374 added to reserved.variables.get

From: Date: Tue, 29 Dec 2009 20:49:42 +0000
Subject: note 95374 added to reserved.variables.get
Groups: php.notes 
Request: Send a blank email to php-notes+get-164449@lists.php.net to get a copy of this message
I created a function to help manage creating a hyperlink and modifying a parameter (or adding it to the query string). It preserves the current location of the parameter in the query string if it already exists, or appends it to the end. It does not duplicate parameters. <?php function changeQueryString($parameter, $value) { $queryString = $_SERVER["QUERY_STRING"]; if($queryString == "") return $parameter."=".urlencode($value); $ampExpl = explode("&", $queryString); foreach($ampExpl as $i => $query) { $equExpl = explode("=", $query, 2); $paramsAndVals[$equExpl[0]] = $equExpl[1]; if($equExpl[0] === $parameter) $key = $i; } $ampExpl[$key] = $parameter."=".urlencode($value); return implode("&", $ampExpl); } ?> Use it, for example, to create a hyperlink like this: <a href="mypage.php?<?php echo htmlspecialchars(changeQueryString("pi", "42")); ?>">Click here to change pi to 42!</a> This will change the query string from (e.g.): a=1&b=2&c&d&pi=17&k=12 to: a=1&b=2&c&d&pi=42&k=12 or a=1&b=2&c&d&k=12 to a=1&b=2&c&d&k=12&pi=42 ---- Server IP: 69.147.83.197 Probable Submitter: 72.208.240.199 ---- Manual Page -- http://www.php.net/manual/en/reserved.variables.get.php Edit -- https://master.php.net/note/edit/95374 Del: integrated -- https://master.php.net/note/delete/95374/integrated Del: useless -- https://master.php.net/note/delete/95374/useless Del: bad code -- https://master.php.net/note/delete/95374/bad+code Del: spam -- https://master.php.net/note/delete/95374/spam Del: non-english -- https://master.php.net/note/delete/95374/non-english Del: in docs -- https://master.php.net/note/delete/95374/in+docs Del: other reasons-- https://master.php.net/note/delete/95374 Reject -- https://master.php.net/note/reject/95374 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#164449) next »