Deleting a query value from a URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Deleting a query value from a URL

Post by hairyjim »

Is it possible to delete a query from a URL?

I tried unset($_get[submit]) but it did not do as I thought. I thought it would remove it from the URL string so all my links on the page that I generate dynamically would appear without &Submit=submit.

Is there some other function I need to look into that may help?

On a side not do people know of any good tutorials on using functions & classes? I feel comfortable with the basics of PHP and would now like to take the next steps in creating more efficient code.

Cheers
Jim
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

post your code. PHP itself does not generate &Submit=submit, it's you code where this parameter is added.
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post by Bennettman »

What I do, is to recreate the string using $_GET, with a simple validation on what's what.

Code: Select all

<?php

foreach ($_GET as $key => $value) {
     if ($key != 'submit') $get_string .= $key.'='.$value.'&';
}

?>
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post by hairyjim »

Weirdan wrote:post your code. PHP itself does not generate &Submit=submit, it's you code where this parameter is added.
Yeah I know that.

I have a form on my page that allows a user to filter the displayed results from a table.

There may be more than 1 page of results after filtering so I wish to be able to pass the filter parameters onto the next page.

I use PHP 5 function http_build_query($_GET) to grab the URL query so I can build it into my page links. I think Bennettman has given me the answer I was looking for.

Another question.

I have employed Bennettman's code to generate my URL query

Now my page links <a href tag looks like this:

Note I changed Bennettman's $get_string var to $geturl to fit in with what I was already working with.

Code: Select all

// Create page number links 
for ($i = 0; $i < $pages; $i++) &#123; 
$url = "pageid=" . $i; 
   if ($i == $pageid) 
   &#123; 
      echo "  <font class="CurrentPage">".($i + 1)."</font>  "; 
   &#125; else &#123; 
      echo "  <span class="Smallarticletext"><a href="test.php?". $geturl ."&". $url ."">".($i + 1)."</a></span>  "; 
         &#125; 
&#125;
So if I filter the page the a href looks like this:
test.php?&product=product1&pageid=1

So when I click on this all the correct parameters are passed on to the next page. But on this next page when i look at the a href it looks like this:
test.php?&product=product1&pageid=1&pageid=2

It appends the same pageid query onto the end of the url. All still works fine I presume because the latter pageid query is dominant.

What do I need to do to stop this though?
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post by Bennettman »

Just add a

&& $key != 'pageid'

to the if statement when making the $geturl. Stops the current $pageid from going in the URL as well as $result.
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post by hairyjim »

Cheers buddy. You have been a great help.

Jim
Post Reply