0

I am using the Facebook Graph API to get the users locale information (updated code):

[more code]
<div id="scoreboard-overview">
    <ul>';
    $num_rows = mysql_num_rows($resulttt);
    $i = 0;
        while($row = mysql_fetch_array($resulttt)) {
            $i = $i + 1;
            $fb_data = json_decode(file_get_contents('http://graph.facebook.com/'. $row['fbID']));
            $fb_locale_str = $fb_data->locale;
            $fb_name_str = $fb_data->first_name;
            $fb_country_str = strtolower(substr($fb_locale_str, -2));
            $flag_uri = '/flags/unknown.gif'; //display a general flag if unknown.
            if (!empty($fb_country_str) && in_array($fb_country_str, $valid_flags) )
                $flag_uri = '/flags/' . $fb_country_str . '.gif';

echo '<li>
        <div class="container">
            <div class="black">
                <img src="https://graph.facebook.com/'. $row['fbID'] .'/picture" alt="" />
            </div>
            <div class="grey">
                '.$i.' '.$fb_name_str.'';
            printf ('<div class="test"><img src="%s" /></div>', $flag_uri); 
    echo '      </div>
            <div class="holder">
                <div class="blue">
                    <p>0<br />'. $row['Time'] .'</p>
                </div>
                <div class="red">
                    <img src="http://ep2.nl/images/star.gif" alt="" />
                </div>
                <div class="yellow">
                    <p>'. $row['Score'] .'</p>
                </div>
            </div> 
        </div>
    </li>';
        }
echo '</ul>
</div>
    [more code]

$row['fbID'] is the users and/or friends Facebook ID (used in a array)

Well this gives a result like: en_US where en is the users language and US is the users country. The country is where I am after.

I have a couple of flag images that look like this: us.png. These flags are named using the ISO3166-1 alpha-2 country codes where appropriate. So I want to trim this en_US to us and put .png behind it so I can show the flag image. But how to do this?

Also a general flag if the locale returns nothing like unknown.png would be nice.

Many thanks,

Maurice

Maurice
  • 1,147
  • 2
  • 21
  • 49

1 Answers1

2

Not too hard to do.

Try this:

$valid_flags = array ('us', 'ca', 'mx', '...') //You'll need to populate this one

$fb_data = json_decode(file_get_contents('http://graph.facebook.com/'. $row['fbID']);
$fb_locale_str = $fb_data->location;
$fb_country_str = strtolower(substr($fb_locale_str, -2));
$flag_uri = '/flags/unknown.png'; //display a general flag if unknown.
if (!empty($fb_country_str) && in_array($fb_country_str, $valid_flags) )
    $flag_uri = '/flags/' . $fb_country_str . '.png';
printf ('<div class="grey"><img src="%s" /></div>', $flag_uri);

As a note, you shouldn't be calling file_get_contents inside of an echo statement.

cpilko
  • 11,792
  • 2
  • 31
  • 45
  • Thanks for the reply @cpilko. I understand what your code is doing and it even has a check for the unknown flag. It's great! Thank you. The only problem I have atm is putting it in my array/result. How to do this properly? I have extended/updated my first post with some more code of my array. Hope you can help me integrate this. Another question: why shouldn't I be calling file_get_contents inside of an echo statement? Is there a better way to do this? And how? Thank you again for your time! – Maurice May 08 '12 at 22:25
  • The reason you don't use file_get_contents inside an echo statement is every time you call it, it makes a call to download that file. In your updated example, you'll be making two complete calls to get the same data. – cpilko May 08 '12 at 23:07
  • As for where to put this, the $valid_flags definition should go in the initialization part of the file. Everything else should go inside your while loop. – cpilko May 08 '12 at 23:11
  • Thanks again @cpilko. Had to do a few adjustments, but it worked. I even added `$fb_name_str = $fb_data->first_name;` to get the other JSON string. Does this fix the file_get_contents correctly? I have updated the code again. If you have more suggestions, please let me know (except the printf part, I just let it in for now to make sure it works). Thanks again! – Maurice May 09 '12 at 08:38