Jump to content

Help with switch case when url contains ..??


azukah

Recommended Posts

i have this code to change $greeting based on the URL. if the URL contains ../es or ../it, $greeting will change to the appropriate language greeting.

 

<?php
switch( dirname( $_SERVER['PHP_SELF'])) {
case '/es':
  	$greeting = 'Hola!';
break;
case '/it':
$greeting = 'Ciao!';
break;
default:
$greeting = 'Hello!';
break;
}
?>

//here's the actual line where I call $greeting

<li><a class="home" href="index.php"><?php echo $greeting; ?></a></li>

 

the code shows "Hello!" which is the default only. regardless of the folder I am in (".../es" or ".../it").. ay ideas??

Link to comment
https://forums.phpfreaks.com/topic/239302-help-with-switch-case-when-url-contains/
Share on other sites

$_SERVER['PHP_SELF'] doesn't point to a directory, it points to the current file.

 

How is your URL going to be setup? Like this: example.com/es/index.php Or like this: example.com/index.php/es

 

If the first, use .htaccess. If the second, try this:

 

$segments = exp('/', $_SERVER['PHP_SELF']);

switch($segments[1])
{
case 'es':
  	$greeting = 'Hola!';
break;
case 'it':
	$greeting = 'Ciao!';
break;
default:
	$greeting = 'Hello!';
break;
}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.