Adding variable to [PHP_SELF] link

I am trying to work out how I can add a variable to PHP_SELF. I have a page in my catalogue which brings up data based on its address.

The address could be catalogue.php or catalogue.php?Genus=3 or catalogue.php?Genus=3&Division=2.

I am having problems because I am trying to add a paging function and can’t seem to get it to work. This is the code I am using:

$self = $_SERVER['PHP_SELF'];

Then later on in the code I am using something like this to provide the link:

$next = " <a href=\“” . $self . “page=$page\”>[Next]</a> ";

The problem I have is when the page is catalogue.php?Genus=3. For some reason PHP_SELF ignores the part after the question mark and so my link ends up being catalogue.php?page=2. I have tried adding an extra bit of code onto the link, using something like:

$self .="?Genus=3";

But then this ends up looking like this: catalogue.php?Genus=3?Page=2, which obviously doesn’t work. I have also tried using [REQUEST_URI] but this has a problem when you go onto another page as always adds to the address and I end up with something looking like catalogue.php?Genus=3&Page=2&Page=3&Page=4, etc., etc.

Does anyone know a way in which I can achieve this?

You just need to be a little smarter in your concatenating, and write a & when you need a & instead of a ?. There’s no automagic solution to this, you need to write an if/else statement or two to construct your URLs.

Take a look at http_build_query()

Thanks for the help. I got a bit scared by the HTTP_BUILD_QUERY and Dan, if you look at the code you will see that changing the ? for a & will not help, but I found another way of solving this. Maybe not in the best possible way but at least it works. Although I was having problems with the following code:

$self = $_SERVER['PHP_SELF'];
{$self .="&Genus=3";}

This would give a result of catalogue.php&Genus=3?Page=2 for example. It always put whatever I was trying to add before the Page number and the question mark so this wouldn’t work.

However I found that doing this later on with a line like this works fine and puts the code in the right order

$prev = " <a href=\\"" . $self . "?Genus=" . $_GET['Genus'] . "&amp;page=$page\\">[Prev]</a> ";

So I’ve just used the GET clauses later on and maybe repeated a bit of information but at least it works!!

Thanks for your help!