0

I'm having an issue removing a particular query string from a url. I want to keep the rest of the queries but simply dump the last one. The problem is the query string could have a different value each time, so I dont know how to do this.

For example my site url might be:

http://sitename.com/index.php?Cat=Bus&Origin=UK

What I want to do, is keep Cat=Bus and remove Origin=UK

So if I try to change the origin to Germany I write:

echo '<a href="'.$_SERVER['REQUEST_URI'].'&Origin=Germany">Germany</a>';

So it takes the page it is on:

http://sitename.com/index.php?Cat=Bus&Origin=UK

Using Request_URI. I want to then strip it of &Origin=* Then once thats done pass it into a string and add whatever the new origin is on the end.

I figure:

$theURI = $_SERVER['REQUEST_URI'];

But I have no idea how to go from start to finish.

$amended = $theURI - '&Origin=*'

and then end up with

echo '<a href="'.$amended.'&Origin=Germany">Germany</a>';

I just don't know how to express that $amended function in PHP.

Could somebody help? This is being used for sorting between countries

3
  • I'd have used str_replace but &Origin will be different each time. It wont always be UK, it could be a whole host of other nations Commented Dec 15, 2012 at 9:30
  • 2
    echo '<a href="/index.php?Cat=' . $_GET['Cat'] . '&Origin=Germany">Germany</a>'; ? Commented Dec 15, 2012 at 9:32
  • Ah I forgot to add - Im reusing the query, so it may have a subCat as well. Which is why I'm using request_uri As on each page it is a sort by country feature. So in the main Category that query would work. But then if the url was http://sitename.com/index.php?Cat=Bus&SubCat=Whatever&Origin=UK The query wouldn't work. So i'd have to add an if SubCat is defined statement, and all the other if statements. It starts to get quite messy. Commented Dec 15, 2012 at 9:39

2 Answers 2

1

If you know the exact variables and they are not that many, you can do with Dale's comment:

echo '<a href="/index.php?Cat=' . $_GET['Cat'] . '&Origin=Germany">Germany</a>';

If not you could use str_replace:

$url = $_SERVER['REQUEST_URI'];
$url = str_replace("origin=" . $_GET["origin"], "", $url);
$url = preg_replace("#&+#", "&", $url);
... 
echo "<a href='" . $url . "&origin=Germany"'>Germany</a>";
Sign up to request clarification or add additional context in comments.

3 Comments

Ah this looks pretty much like what I'm after! In fact I've tested it. Perfect thank you. Pasting exactly what worked - I didnt think of using $_GET with the str_replace nor preg_replace, thanks! $url = $_SERVER['REQUEST_URI']; $url = str_replace("Origin=" . $_GET["Origin"], "", $url); $url = preg_replace("#&+#", "&", $url); echo 'Sort By Country:'; echo '<br>'; echo '<a href="'.$url.'Origin=UK">United Kingdom</a>'; echo '<br>'; echo '<a href="'.$url.'Origin=Germany">Germany</a>';
Oh, as I dont know much about preg_replace, Could you tell me what these do? "#&+#"
It is a regular expression. I don't know much about them either, but this one checks to see if there is more than one '&' and replaces it with only one '&'. Useful if origin is not the end parameter. To be honest I'm not sure if it is needed or a & is needed in str_replace as well, but should do the job :)
1

Depending on how you're getting the origin (static url vs dynamic url), it's a simple manipulation of the $_GET superglobal.

$url = $_SERVER['REQUEST_URI']; //edit: I usually use $_SERVER['PHP_SELF'], so try this if REQUEST_URI fails
$get_param = $value; //$value can be germany, england, poland, etc etc
$url .= "&Origin=".$get_param;
echo "<a href='".$url."'>$get_param</a>";

I hope this helps

2 Comments

The issue I'd have with that is I'd have to define $value each time. Its a thing where I can click on the side to sort entries by country. So I might have say two options: if (!$_GET["Origin"]){ echo 'Sort By Country:'; echo '<br>'; echo '<a href="'.$_SERVER['REQUEST_URI'].'&Origin=UK">United Kingdom</a>'; echo '<br>'; echo '<a href="'.$_SERVER['REQUEST_URI'].'&Origin=Germany">Germany</a>'; } However if I've got an origin set already, I want to change that origin. But if I'm ive got a &subCat after the regular &cat id have issues
It's going to have to be defined somewhere though, whether user input or hardcoded. I'd put an edit in your question mentioning it's being used for sorting, that might help out. I'll rethink my answer though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.