A way to get and edit GET variables to create hyperlinks

Say I have a URL like somefile.php?sort=id&way=desc .

I want to write a function (or use one already done) that would allow me to add the following variables to the URL and set which I want to delete.

I thought of something like function editGetVar("$add","$leave") , where $add will be an array with new variables to add to the URL, and $leave will be an array with variables that should remain in the URL.

Example:

 somefile.php?sort=id&way=desc&buyer=retailer 

and I want to remove the โ€œbuyerโ€ and add the โ€œactionโ€, then a href will look like this:

 <a href="somefile.php?sort=id&way=desc&action=edit"> 

I would appreciate any ideas from you.

+6
php global-variables get hyperlink
source share
3 answers

Use http_build_query :

 <?php unset($_GET['buyer']); $_GET['action'] = 'edit'; print '<a href="somefile.php?' . http_build_query($_GET) . '">!!</a>'; ?> 
+10
source share

I believe that you can split the URI with $parts = parse_url($my_uri) , manipulate the resulting array and http_build_query it back with the http_build_query function.

+4
source share

Example:

 $url = '?'; foreach( $_POST as $key => $value ) { $url .= $key . '=' . $value . '&'; } 

You can add / change a variable as:

 $_GET[ 'sort' ] = 'asc'; 

You can delete as:

 unlink( $_GET[ 'sort' ] ); 

You can transfer it to the function yourself;)

0
source share

All Articles