2

I'm wondering if there is a technique or function to do this or if I'm just going to have to have a ton of IF statements/arrays here-

I have a page called products.php and a few different filters I add in the query string. All the filters (if chosen) could look like this:

products.php?cat=chairs&type=pine&search=Vienna+Range

Is there a simple way to build the query for use again?

I'm going to have buttons/a search box to change the filters I'm using, so will I have to build the URL and query up again for each filter?

For example I'd like to have:

<a href="products.php?cat=tables">Tables</a>
<a href="products.php?cat=beds">Beds</a>
<a href="products.php?cat=chairs">Chairs</a>

but also build the query so that it remembers my search term, wood type and range; so clicking on "Tables" would take me to

products.php?cat=chairs&type=pine&search=Vienna+Range.
1
  • 4
    What about the php function http_build_query? php.net/http_build_query Commented Oct 9, 2011 at 6:44

1 Answer 1

3

You can write something like:

<?php
$params = array(
    'cat'    => 'chair',
    'type'   => 'pine',
    'search' => 'Vienna Range',
);

print_r(http_build_query($params) . PHP_EOL);

You'll get this:

cat=chair&type=pine&search=Vienna+Range
Sign up to request clarification or add additional context in comments.

3 Comments

So I would have to do build the query like that for each filter?
Well, I would do something like this: $params = array('new_search_param' => 'value'); $params = array_merge($_GET, $params); This way, the old search params will be preserved.
That's a bit over my head for someone at my level, but you made me look at http_build_query again which gives me what I want. I just have to check if a value it is entered as "NULL". Thank you!

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.