planetalk wrote:If I wanted to add a new batch of countries (like those that use the Euro), do I just carry on with the same lines of code starting with 'else if'?
You could. An alternative to a lot of if/else statements is a switch:
Code: Select all
switch ($countrycode) {
case 'AU':
case 'NZ':
header('Location: http://www.planetalkguitar.com/orderau.html');
break;
case 'GB':
header('Location: http://www.planetalkguitar.com/orderuk.html');
break;
default:
header('Location: http://www.planetalkguitar.com/orderus.html');
break;
}
You could also rewrite the code to make it easier to deal with - less copy and paste.
Code: Select all
$pages = array(
'AU' => 'orderau.html',
'NZ' => 'orderau.html',
'GB' => 'orderuk.html',
0 => 'orderus.html' // default
);
$page = (isset($pages[$countrycode]) ? $pages[$countrycode] : $pages[0]);
header('Location: http://www.planetalkguitar.com/' . $page);
This way you only have to update the $pages array.
Even easier would be to make the files be orderau.html, ordernz.html, and ordergb.html (and orderus.html) according to the country code and do
Code: Select all
$page = 'order' . strtolower($countrycode) . '.html';
if (is_file($page)) {
header('Location: http://www.planetalkguitar.com/' . $page);
} else {
header('Location: http://www.planetalkguitar.com/orderus.html');
}
which redirects to a order*.html file if present or the US one if not.
planetalk wrote:Oops ... I just changed the code to your suggestion and this came up:
Fatal error: Cannot use object of type stdClass as array in /home/ptguitar/public_html/order.php on line 3
I switched back to the old code. Any idea why that happened?
My fault. Should be
Code: Select all
$a = json_decode(file_get_contents('http://www.geoplugin.net/json.gp?ip='.$_SERVER['REMOTE_ADDR']), true);
By default json_decode turns stuff into objects, which means the next line would need to have "$a->geoplugin_countryCode". You could do that, or you could pass that "true" to make it turn stuff into arrays instead.