Remove older $_GET from url

I've got some problems with my url on my website. I'm trying to get a link with the given GET parameters, but i'm getting my previous parameter aswell.

My url looks like this:

www.cdwinkel.dev/search-results?genre=Pop&medium=DVD&medium=Single .

It should be:

www.cdwinkel.dev/search-results?genre=Pop&medium=Single .

I'm running the following code:

$data['url'] = createurl();   

function createurl(){
    $i = 1;
    $string = "?";
    $keys = array_keys($_GET);
    foreach($_GET as $get){
        if($get != ""){
        $string .= $keys[$i] . "=" . $get ."&";
        $i++;
        }
    }
    $string = rtrim($string, "&");
    return $string;
}

$i = 1, because my first value in my array is empty.

And my button looks like this:

<a href='".$data['url'].'&medium='.$names[$i]."'>

I guess I should'nt set &medium=.$names[$i] in the href tag, but I wont get the new $names[$i] in my function, so I won't get a new url if i wont add it in.

I'm looking forward to your responce.

Sincerely, Kars Takens

Answers


At this point i've created an array with the right arraykeys and values.

 $url = array_slice($_GET, 1);

This returns the following array:

array (size=2)
    'genre' => string 'Pop' (length=3)
    'medium' => string 'DVD' (length=3)

After this I decoded this into a new string:

genre=Pop&medium=DVD

I got 6 button which I created in a foreach loop, but i'm getting &medium='VALUE' Twice. This only happens after the first time. So the first time my button works well.

<?php
$names = array_keys($data['tellen']);
$i = 0;
foreach($data['tellen'] as $m){
     echo "<li><a href='search-results?".$data['url'].'&medium='.$names[$i]."'>". $names[$i] ." <span class='product-amount'>(". $m[0]->count. ")</span></a></li>";
     $i++;
}

Hopefully you can help me further with this information.

I solved my problem by adding this in my forloop:

foreach($data['tellen'] as $m){
if(isset($_GET['medium'])){
     unset($_GET['medium']);
     $url = array_slice($_GET, 1);
     $data['url'] = urldecode(http_build_query($url));
     }
echo "<li><a href='search-results?".$data['url'].'&medium='.$names[$i]."'>". $names[$i] ." <span class='product-amount'>(". $m[0]->count. ")</span></a></li>";
    $i++;
}

Need Your Help

Using Twill from Python to open a link: " 'module' object has no attribute 'Popen' " What is it?

python google-app-engine popen twill

I have downloaded and installed Python 2.5.4 on my computer (my OS is Windows XP), downloaded “Goggle App Engine Software Development Kit” and created my first application in Python, which was a