2

Currently I am using the php geoip_country_code_by_name function to serve up different content for different countries from an array which looks like this:

<?php

    $content = array(
        'GB' => array(
            'meta_description'  => "Description is here",
            'social_title'      => "Title here",
            'country_content_js'   => "js/index.js",
        ),
        'BR' => array(
            'meta_description'  => "Different Description is here",
            'social_title'      => "Another Title here",
            'country_content_js'   => "js/index-2.js",
        ),
    );

?>

But I only have specific content for Brazil and Great Britain. I want any other country accessing the page to be served a default array of content which will differ from BR and GB.

Is there a way to create a rule that serves up a default set of content to any country that is not specified in my array?

1 Answers1

1
$content = array(
    'GB' => array(
        'meta_description'  => "Description is here",
        'social_title'      => "Title here",
        'country_content_js'   => "js/index.js",
    ),
    'BR' => array(
        'meta_description'  => "Different Description is here",
        'social_title'      => "Another Title here",
        'country_content_js'   => "js/index-2.js",
    )
);

You could reference a key using another 'Default' key like so;

$content['Default'] =& $content["GB"];
var_dump($content);
exit;

Alternatvly, if you ordered the value you return from DB or wherever, you could read the first entry to the array like this; $aDefault =& $content[array_keys($content)[0]];

Or you could define a default language and read that array key, however unlike the previous method it must be in the array.

// define default 
define("DEFAULT_LANGUAGE", 'GB');

// would need to guarentee its there
$aDefault =& $content[DEFAULT_LANGUAGE];

Last you could combind the above so if it cant find that language you could just use the first avaliable;

// define, can be placed in an included config folder
define("DEFAULT_LANGUAGE", 'GB');

$content = array(
    'GBs' => array(
        'meta_description'  => "Description is here",
        'social_title'      => "Title here",
        'country_content_js'   => "js/index.js",
    ),
    'BR' => array(
        'meta_description'  => "Different Description is here",
        'social_title'      => "Another Title here",
        'country_content_js'   => "js/index-2.js",
    )
);

// does the default language exist?
if( isset($content[DEFAULT_LANGUAGE]) ){
    // yes, create a default array key and reference the required element in the array
    $content['Default'] =& $content[DEFAULT_LANGUAGE];
}else{
    // no, create a default array key and reference the first element
    $content['Default'] =& $content[array_keys($content)[0]];
}

var_dump($content);
exit;
atoms
  • 2,993
  • 2
  • 22
  • 43
  • Thank you for your answer atoms this is very helpful! I am rather new to php so I just have a simple question. Is this piece of code you wrote: define("DEFAULT_LANGUAGE", 'GB'); the actual way that I would define my default language in php? – sandman human man Oct 02 '16 at 08:45
  • no problem sandman. The define keyword allows you to set a string, in this case the text `GB` to be accessable across the runtime of that scrip/file. See http://php.net/manual/en/function.define.php, I've also updated the answer, the last block of code should resolve your issue – atoms Oct 02 '16 at 08:48
  • Oh ok thats so simple what a great solution! Is there a specific file in which this last block of code should be placed? Or can it run from anywhere? My apologies again if this is another simple question. – sandman human man Oct 02 '16 at 08:54
  • have updated the code. I would place the define in a file like config.php and include it at the top of each page. I would also do things different and check the language once, then request that language. This was as your site grows you wont be loading many different copies of the information. However for you current application, I would check just before you request the language – atoms Oct 02 '16 at 09:04
  • Incredible attention to detail and extremely helpful answer. You have relieved a lot of stress from the life of a front end developer who got himself mixed up in backend coding haha Thank you, thank you, thank you, for your time and your work. – sandman human man Oct 02 '16 at 09:07
  • Pleasure sandman, glad it's helped. Please up vote if it's useful, have a nice day – atoms Oct 02 '16 at 09:13
  • I have upvoted but I do not have enough reputation for it to display publicly. Sorry about that. If I could bother you with one more question: my coworker has suggested that loading a javascript (last line of array) in an array like this is not a good way to do things. Would you agree? To me it seems perfectly logical. – sandman human man Oct 02 '16 at 09:16
  • Thank you, this is php not JavaScript. And as said I would only load one language, detect that first, then only request the language that you need – atoms Oct 02 '16 at 09:19
  • I am aware that this is php not javascript but the last line of each of my language arrays are javascript files: " 'country_content_js' =>'js/index.js' " is this an ok way to load in separate js files? (last comment I promise haha) – sandman human man Oct 02 '16 at 09:24
  • ah I see. I guess it depends on the enviroment and what those files contain/how there used. If they are small you could consider compressing them together serving one file and caching it. This way you could serve one file once. – atoms Oct 02 '16 at 09:30
  • Again very helpful. Thank you Atoms! Have a great day! – sandman human man Oct 02 '16 at 09:31