...

View Full Version : Switch Based On Contents Of URL



Pennimus
09-22-2005, 12:04 PM
Hi all,

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

So for example -



case '613':
echo 'bla';
break;


- would be picked up by www.example.com/product.php?pid=613

Cheers

Yaggles
09-22-2005, 12:18 PM
That's actually not too hard:


$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...

e-Raser
09-22-2005, 12:18 PM
If you use a switch statement, it should do it automatically.

switch($pid) {
case "613":
echo "bla";
break;
}
I was beaten to it ^^0

Pennimus
09-22-2005, 01:25 PM
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.

The Doc
09-22-2005, 01:29 PM
you really should use the $_GET array instead!


switch($_GET['pid']){
...
}

Yaggles
09-23-2005, 11:09 AM
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.

Fou-Lu
09-23-2005, 01:00 PM
_* 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:


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.

Yaggles
09-23-2005, 02:03 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum