0

I'm puttings filters in links with GET variables like this: http://example.com/list?size=3&color=7 and I'd like to remove any given filter parameter from URL whenever a different value for that particular filter is selected so that it doesn't, for example, repeat the color filter like so:

http://example.com/list?size=3&color=7&color=1

How can I if(isset($_GET['color'])) { removeGet('color'); } ?

Xeen
  • 6,955
  • 16
  • 60
  • 111
  • 1
    Just a suggestion, but its probably better to handle that before those variables get put in as GET variables in the first place. – georaldc Jun 05 '15 at 20:13
  • @georaldc not sure what you mean by that – Xeen Jun 05 '15 at 20:15
  • 1
    How are the GET variables being populated? Is it a custom link you build out? A GET form with inputs like "size" and "color"? How did you end up with multiple "color" variables? Since you control how you are putting those variables, you should at least be able to control which specific "color" filter value you would want to use as well. The server will only see 1 GET variable of a specific name (unless it was an array), so give it what you really want to use. – georaldc Jun 05 '15 at 20:20
  • Possible duplicate of [Function to remove GET variable with php](https://stackoverflow.com/questions/7195342/function-to-remove-get-variable-with-php) – Nico Haase Jul 30 '19 at 15:49

5 Answers5

1

You can use parse_url and parse_str to extract parameters like in example below:

$href = 'http://example.com/list?size=3&color=7';

$query = parse_url( $href, PHP_URL_QUERY );
parse_str( $query, $params );

// set custom paramerets
$params['color'] = 1;

// build query string
$query = http_build_query( $params );

// build url
echo explode( '?', $href )[0] . '?' . $query;

In this example explode() is used to extract the part of the url before the query string, and http_build_query to generate query string, you can also use PECL http_build_url() function, if you cannot use PECL use alternative like in this question.

Community
  • 1
  • 1
Danijel
  • 12,408
  • 5
  • 38
  • 54
0

You can't remove variables from GET request, just redirect to address without this var.

if (isset($_GET['color'])) {
    header ('Location: http://www.example.com/list?size=' . $_GET['size']);
    exit;
}

Note:
in URL http://example.com/list?size=3&color=7&color=1 is just one $_GET['color'], not two. Only one of them is taken. You can check, is $_GET['key'] exists, but you don't know how many of them you have in your URL

pavel
  • 26,538
  • 10
  • 45
  • 61
0

So, assuming I'm understanding your question correctly.
Your situation is as follows:
- You are building URLs which you put into a webpage as a link ( <a href= )
- You are using the GET syntax/markup (URL?key=value&anotherkey=anothervalue) as a way to assign filters of some sort which the user then receives when they click on a given link

What you want is to be able to modify one of the items in your GET parameter list (http://example.com/list?size=3&color=7&color=1) so you have only one filter key but you can modify the filter value. So instead of the above you would start with: (http://example.com/list?size=3&color=7) but after changing the color 'filter' you would instead have http://example.com/list?size=3&color=1).

Additionally you want to do the above in PHP, (as opposed to JavaScript etc...).

There are a lot of ways to implement the change and the most effective way to do it depends on what you are already doing, most likely.
First, if you are dynamically producing the HTML markup which includes the links with the filter text, (which is what it sounds like), then it makes the most sense to create a PHP array to hold your GET parameters, then write a function that would turn those parameters into the GET string.
New filters would appear when a user refreshed the page, (because, if you are dynamically producing the HTML then a server request is required to rebuild the page).
IF, however, you want to update the link URLs on a live page WITHOUT a reload look into doing it with JavaScript, it will make your life easier.
NOTE: It is likely possible to modify the page, assuming the links are hard coded, & the page is hard coded markup, by opening the page as a file in PHP & making the appropriate change. It's my opinion that this would be a headache and not worth the time & effort AND it would still require a page reload (which you could NOT trigger yourself).

Summary
If you are writing dynamic pages with PHP it shouldn't be a big deal, just create a structure (class or array) and a method/function to write that structure out as a GET string. The structure could then be modified according to your desire before generating the page.
If, however, you are dealing with a static page, I recommend JavaScript (either creating js structures to allow a user to dynamically select filters or utilizing AJAX to build new GET parameter lists with PHP and send that back to the javascript). (NOTE: I am reminded that I have done something along the lines of modifying links on-the-fly for existing pages by intercepting them before they are displayed to the user [using PHP] but my hands were tied in other areas and I would not recommend it if you have a choice AND it should be noted that this still required a reload...)

MER
  • 1,455
  • 20
  • 25
0

Try doing something like this in your back-end script:

$originalValues=array();

foreach($_GET as $filter=>$value)
{
   if(empty($originalValues[$filter]))
     $originalValues[$filter] = $value;
}

This may do what you want, but it feels hackish. You may want to revise your logic.

Good luck!

angelcool.net
  • 2,505
  • 1
  • 24
  • 26
0

just put a link/button send the user to index... like this.

<a class="btn btn-primary m-1" href="http:yoururl/index.php" role="button">Limpar</a> 
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31516805) – Florian Rival Apr 14 '22 at 07:59