Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6031253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:09:23+00:00 2026-05-23T05:09:23+00:00

I have a small issue with manipulating the current URL query string to add

  • 0

I have a small issue with manipulating the current URL query string to add an extra parameter at the end.

Per example, say there’s a category layout for products, the URL would be:

index.php?category=3&type=5

Now, on that page I have a link for a layout that is either a table or a grid. In those URLs I currently have:

<a href="index.php?<?php echo preg_replace($array,'',$_SERVER['QUERY_STRING']); ?>&layout=grid" ...

Then, I do the same for the table href as well. Also in my array I have just:

$array = array ( '/&layout=table/', '/&layout=grid/' )

Is this the right way, or is there a better way for doing this? I’m asking because without preg_replace, it will continue adding that same layout parameter everytime it is clicked, so it will also show the previous parameter, then the next, then the next.. without removing the previous layout parameters.

Any insight on this will be much appreciated.

EDIT:
Thanks to the answers below, I have created a little function:

function buildQuery($key,$value) {
$params = $_GET;
$params[$key] = $value;

return http_build_query($params);
}

Then its only a matter off:

<a href="index.php?<?php echo buildQuery('layout','grid'); ?>">grid</a>

this might seem pointless but i like to have my view / template files without the extra set vars. Im a clean freak. I might even return the ‘index.php?’ with it just so i can be more lazy, anyways something to play with now :)..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:09:24+00:00Added an answer on May 23, 2026 at 5:09 am

    If you want to modify the query string, it’s easier to simply modify the GET variables and rebuild the query string:

    $params = $_GET;
    $params['layout'] = 'new_layout';
    

    Then:

    <a href="index.php?<?php echo http_build_query($params); ?>">...</a>
    

    Although you could also do:

    <a href="index.php?<?php echo preg_replace('/(^|&)layout=[^&]*/', '\\1layout=new_layout', $_SERVER['QUERY_STRING']; ?>">...</a>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a small issue with converting a string to a boolean when
I am just new to programming in Unix and have a small issue that
can anyone help, i have a small issue, i have an interface and also
We're having a small issue and could use some help - we have the
everything is working fine with my jqgrid except a small issue. i have defined
I have a small issue, I had my SIP client working, and I changed
I have a simple, general question regarding a real small issue that bothers me:
I've a small issue with URL generation & routing under ASP.NET MVC 2. My
I have a small issue with one of my projects, I have an entity
I have small issue. I create 2D int array. When I cout it I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.