Re: $_GET strings seperation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 5/26/07, Navid Yar <nyar@xxxxxxxxxxxxxxxxxx> wrote:
Hello Everyone,

I have a problem with GET strings. I use
$_SERVER["REDIRECT_QUERY_STRING"] to get the value-pairs in the URL.
The problem is that there is a cID variable that keeps appending itself
to the string continuously everytime someone clicks on a different
category link on the website. For example, instead of this:


http://www.someexample.com/admin/index.html?cID=42&somevar=value&somevar2=value2

it keeps appending another cID to it everytime it goes to a different
link, like this:


http://www.someexample.com/admin/index.html?cID=42&cID=39&cID=44&cID=37&somevar=value&somevar2=value2

I know that this is happening because I'm appending it with the dot (.)
but is there a way to just inject a single cID and still have the rest
of the value-pairs available? Something built into PHP, maybe a different
predefined variable I don't know about? Or, do I have to make a
complex function to separate each out and then put it back together
again like humpty dumpty? Is there an easier way to do this and still
have a single cID variable in the GET string? Thanks in advance.

Is this what you're doing:

<code>
$cid = getCid(); // However you do set the new, included cid
$newlink = 'http://www.someexample.com/admin/index.html?'.$cid.
$_SERVER["REDIRECT_QUERY_STRING"];
</code>

???

If this is similar to what you're doing, this is a fairly problematic way
for you to insert a replacement property in a query string. An example of a
way to get a new query string:

<code>
function getNewQueryString($arr) {
   $store = Array();
   foreach ($_GET as $key=>$val) {
       foreach ($arr as $k=>$v) {
           if (isset($_GET[$k])) {
               $store[$key] = $v;
           }
       }
       if (!isset($store[$key])) {
           $store[$key] = $val;
       }
   }
   $i = 0;
   $str = '?';
   $count = count($store);
   foreach ($store as $key => $val) {
       $amp = $count-1 !== $i ? '&amp;' : '';
       $str .= "{$key}={$val}{$amp}";
       $i++;
   }
   return $str;
}
$query = Array('cID'=>42);
$newlink = "http://www.oompaloompa/land.php".getNewQueryString($query);
echo("<p>$newlink</p>");
</code>

What you need to do is transcribe your $_GET string to a new version,
replacing the current values that need replacing while retaining all other
values. To do this, loop through the $_GET global array, replace those that
match $_GET keynames with the new data, and then rebuild the query into a
string for inclusion in the link.

I'll leave it to you figure out how to add new values that are not replaced
($query=Array('cID'=>51,'doesnotexistyet'=>'completelynewvalue'), for
instance). Also, the above is an example; there are certainly many other
ways to do what is done above (such as replacing the last foreach loop with
an implode() call). There are some strictly unnecessary things done above,
in other words, but I left them in to show what really is happening (and
needs to be done).

--
Jared Farrish
Intermediate Web Developer
Denton, Tx

Abraham Maslow: "If the only tool you have is a hammer, you tend to see
every problem as a nail." $$

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux