Amy
Amy

Reputation: 946

Change $_GET variable in a hyperlink rather than appending it

I have a software download page where you can bring up different categories of software. Like statistics Software, office software, security software and so on. The results can be filtered down via GET like url.com?type=office will bring you to the office software.

I also have links that are meant to filter it down by operating system like so url.com?type=office&os=windows

But how do I have a link like so <a href="url.com?os=windows">Only Windows</a> While preserving all the previous selections like software type while at the same time not appending the url and avoiding any situation like this

url.com?type=office&os=windows&os=windows&os=linux

Any ideas?

Thanks!

Upvotes: 0

Views: 100

Answers (3)

jedwards
jedwards

Reputation: 30260

I'd probably first write a function[1]:

function buildURL($base, $key, $val)
{
    $data = $_GET;
    $data[$key] = $val;
    return sprintf('%s?%s', $base, http_build_query($data));
}

This copies the current $_GET array into a temporary array ($data), sets/overrides the $key value of the array with the value $val, then rebuilds and returns the URL.

Then for each link you want to generate, you could use

<a href="<?= buildURL('url.php', 'os', 'windows'); ?>">Windows Only</a>

The above link would be generated the same, regardless of whether os was set (to anything) or unset. It would also preserve any other key value pairs in the query string.

In other words:

Current Query String                Generated Link
url.php                    =>       url.php?os=windows
url.php?os=linux           =>       url.php?os=windows
url.php?type=office        =>       url.php?os=windows&type=office

[1] Code is untested, but thats the general idea.


Additionally, you could modify the function to something like:

function buildURL($base, $overrides)
{
    $data = array_merge($_GET, $overrides);
    return sprintf('%s?%s', $base, http_build_query($data));
}

which you would then call with something like:

<?= buildURL('url.php', array('os'=>'windows', 'type'=>'office')); ?>

If you wanted to "override" multiple key/value pairs at once.


Alternatively, you could use some combination of hidden form elements and Javascript, but a strictly server-side implementation seems better.

Upvotes: 2

dcclassics
dcclassics

Reputation: 896

$_SERVER['REQUEST_URI'] will return the url with all query strings attached. You can do something like the following:

<?
$url = $_SERVER['REQUEST_URI'];
$url = substr($url, strpos($url, "?") + 1);  
parse_str($url, $url);

var_dump($url);
?>

This will create an array full of your values array(3) { ["os"]=> string(7) "windows" ["type"]=> string(6) "office" } etc. You can use http_build_query to rebuild your query based off of these values.

Alternately, if you use parse_str($url, $url); instead, it will set variables to each of the query names and values. (i.e. $os = windows;). You can then check to see if these are set and build your url accordingly with a case or just if statements. Let me know if you need help with that.

Upvotes: 0

AndiPower
AndiPower

Reputation: 853

You can create your links like that

$type = $os = "";

if(isset($_GET["type"])){
  $type = $_GET["type"];
}

if(isset($_GET["os"])){
  $os = $_GET["os"];
}

$link = "<a href='url.com?type=$type&os=$os'>Some Text </a>";
echo $link;

Upvotes: 1

Related Questions