SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding variable to [PHP_SELF] link

    Hi,
    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:

    Code:
    $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:

    Code:
    $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?

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Take a look at http_build_query()

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    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:

    Code:
    $self = $_SERVER['PHP_SELF'];
    if(isset($_GET['Genus']))
    {$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

    Code:
    $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!
    Russ


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •