Re: [PHP] Re: $_GET strings seperation



Thanks so much Jarred. It helps me learn more when there's an
explaination on how the code works. I'll play around with it, change
it a bit and give that a try. Take care...

P.S. -- I'm in Arlington, TX



On 5/26/07, Jared Farrish <farrishj@xxxxxxxxx> wrote:
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." $$

.



Relevant Pages

  • Re: [File Input Module]Replacing string in a file
    ... replacing the string in the file. ... If you're processing a single file, using fileinput is overkill, and slow ... replace PYTHON (Print on the console) but not physically write to the ...
    (comp.lang.python)
  • Re: Find & Replace Whole Words
    ... I do not think Excel has the functionality you require built-in. ... string or a whole string, NOT a whole word within a string. ... I am sure there are ways do this in VBA, but I cannot think of anything ... and the same for the replacing string. ...
    (microsoft.public.excel.programming)
  • Re: Computability and logic
    ... as regards a mathematical formulation of a substring ... substring in a string with another substring? ... result of substituting a term for a variable in a formula'. ... As I recall, he doesn't get into replacing term for term, ...
    (sci.logic)
  • Re: EXERCITIUM TONALE MONTH - distractions and a mystery
    ... I've just replaced the file at the above link with a third section (6 MB mpg file now), which coincidentally happens to be the third section, of the mystery piece. ... This is another faster section and while not as intensely beautiful as the slower section it is replacing it nevertheless has a certain charm that should appeal to all those people who play for the pleasure of playing. ... I seem to have managed to stumble through the 1st finger 1st fret 1st string pull off to open first string ... For this video the closest thing at hand of half decent shape and sturdiness was an unopened plastic container of Liquid Plumber, and everything was going fine with the LP until at some point of my total absorption in the task at hand the plastic container split open and poured its contents out onto the floor. ...
    (rec.music.classical.guitar)
  • Re: $_GET strings seperation
    ... have a single cID variable in the GET string? ... for you to insert a replacement property in a query string. ... foreach { ... replacing the current values that need replacing while retaining all other ...
    (php.general)