0

I have a page that lists out items according to numerous parameters ie variables with values.

listitems.php?color=green&size=small&cat=pants&pagenum=1 etc.
To enable editing of the list, I have a parameter edit=1 which is appended to the above querystring to give:

listitems.php?color=green&size=small&cat=pants&pagenum=1&edit=1 So far so good.

When the user is done editing, I have a link that exits edit mode. I want this link to specify the whole querystring--whatever it may be as this is subject to user choices--except remove the edit=1.

When I had only a few variables, I just listed them out manually in the link but now that there are more, I would like to be able programmatically to just remove the edit=1.

Should I do some sort of a search for edit=1 and then just replace it with nothing?

$qs = str_replace("&edit=1, "", $_SERVER['QUERY_STRING']);
<a href='{$_SERVER['PHP_SELF']}?{$qs}'>return</a>;

Or what would be the cleanest most error-free way to do this.

Note: I have a similar situation when going from page to page where I'd like to take out the pagenum and replace it with a different one. There, since the pagenum varies, I cannot just search for pagenum=1 but would have to search for pagenum =$pagenum if that makes any difference.

Luke
23.7k31 gold badges121 silver badges201 bronze badges
asked Oct 24, 2014 at 13:32
1
  • 3
    Use parse_str to turn it into an array that you can cleanly work with and then http_build_query to turn it back to a query string. Commented Oct 24, 2014 at 13:37

1 Answer 1

7

You can use parse_str() to parse the query string, remove the unwanted parts and build the new one via http_build_query() like this

parse_str($_SERVER['QUERY_STRING'], $params);
unset($params['edit']);
$new_query_string = http_build_query($params);
answered Oct 24, 2014 at 13:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.