How to append a search query

4 posts by 2 authors in: Forums > CMS Builder
Last Post: March 23, 2010   (RSS)

By Perchpole - March 19, 2010

Ok, All -

Here's another intersting one that might come in handy...

How do I append a search query?

Let's say I have a URL like this...

www.wibble.com/index.php?range=cars

I want to create a new link on the page which takes the current URL and extends it by adding...

&colour=blue

The result is a link which reads...

www.wibble.com/index.php?range=cars&colour=blue

I apreciate that I could just create a new search from scratch - ?range=cars&colour=blue - but in this instance I want to see if I can selectively add elements to the URL as I get deeper into the search.

Do-able?

:0)

Perchpole

Re: [Perchpole] How to append a search query

By Chris - March 22, 2010

Hi Perchpole,

You can use @$_SERVER["QUERY_STRING"] to get the search query for the current page. Use it like this:

<a href="?<?php echo @$_SERVER["QUERY_STRING"] ?>&colour=blue">

Does that help? Please let me know if you have any questions.
All the best,
Chris

Re: [Perchpole] How to append a search query

By Chris - March 23, 2010

Hi Perchpole,

If you need to construct an absolute URL, your code is the way to do it. If all you need is a relative URL, what I posted above is a little simpler.

If both of these links where found on the page http://example.com/hello/world.php?range=cars, they would both result in the browser going to the same address:

<a href="?range=cars&colour=blue">relative</a>

<a href="http://example.com/hello/world.php?range=cars&colour=blue">absolute</a>


I hope this helps. Please let me know if you have any questions.
All the best,
Chris