Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts

    Switch Based On Contents Of URL

    Hi all,

    For a switch statement, how do I write a switch expression that says 'if the URL contains this'?

    So for example -

    PHP Code:
    case '613':
    echo 
    'bla';
    break; 
    - would be picked up by www.example.com/product.php?pid=613

    Cheers

  • #2
    Regular Coder
    Join Date
    Jan 2005
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's actually not too hard:
    PHP Code:
    $pid $HTTP_GET_VARS['pid'];
    switch(
    $pid)
    {
    case 
    '613':
    echo 
    'blah';
    break;

    But if you don't want to have to handcode each product in, it might be easier to use mysql database and fetch the data and then not do a switch...

  • #3
    New Coder
    Join Date
    Jul 2005
    Location
    Perth, Australia. Age: 13
    Posts
    81
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you use a switch statement, it should do it automatically.
    PHP Code:
    switch($pid) {
    case 
    "613":
    echo 
    "bla";
    break;

    I was beaten to it ^^0
    Last edited by e-Raser; 09-22-2005 at 01:20 PM.
    I lost my signature, have you seen it?
    I have the power to erase you...

  • #4
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts
    Thanks very much, switch($pid) is exactly what I need.

    if you don't want to have to handcode each product in, it might be easier to use mysql database and fetch the data and then not do a switch...
    Fortunately, it's only a few pages I need to do, making the switch much easier to set up.

  • #5
    New Coder
    Join Date
    Sep 2005
    Location
    Berlin, Germany
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you really should use the $_GET array instead!

    PHP Code:
    switch($_GET['pid']){
    ...


  • #6
    Regular Coder
    Join Date
    Jan 2005
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, but $_GET doesn't work on some servers... it depends on the php.ini file and one of the vars in it. I am used to using $HTTP_GET_VARS as it is faster for me to type, because I am more used to it.

    But if it works, I guess the shortcut is fine too. thanks for pointing that out.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    _* is superglobal. HTTP_*_VARS is NOT superglobal. Always use _* variables. Superglobals are NOT dependant on ini directives, they are dependant on php version. You are referring to the register_long_arrays directive which allows registration of HTTP_*_VARS. With this directive set to 'off', yeah you got it, HTTP_*_VARS does not exist.
    If the server is too old to cope with it, then create them:
    PHP Code:
    if (PHP_VERSION '4.1.0')
    {
         
    $_GET = &$HTTP_GET_VARS;
         
    $_POST = &$HTTP_POST_VARS;
         
    $_COOKIE = &$HTTP_COOKIE_VARS;
         
    $_SERVER = &$HTTP_SERVER_VARS;
         
    $_ENV = &$HTTP_ENV_VARS;
         
    $_FILES = &$HTTP_POST_VARS;
         
    $_SESSION = &$HTTP_SESSION_VARS;
         
    $_REQUEST array_merge($_GET$_POST$_COOKIE);

    Simple as that. Superglobals can still be used as they are, however within functions you will now need to globalize your _* vars in order for 'old' versions of php to cope.
    Always use superglobals as opposed to their cousins, the depreciated HTTP_*_VARS.
    Last edited by Fou-Lu; 09-23-2005 at 01:02 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #8
    Regular Coder
    Join Date
    Jan 2005
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well... ok. I guess I got mixed up. I just read on phpBB forums that they were switching to HTTP_*_VARS (this was a while back) and I guess I got confused at that point.

    So I'm wrong, you're right. Use _* from now on!

    Thanks!
    -Yaggles


  •  

    Posting Permissions

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