0

I found this nifty code http://snipplr.com/view/36868/ the only difference is that my database displays differently, how can I display each of my countries when my database is set up this way. Here is the code:

 if( $country = 'Afghanistan' ) $code == 'AF';

I figured that, if not correct me the top should be included this way. Here is the code:

  $country = $row['country'];

If that is true I believe although here is where I start getting some troubles It should return the code instead of the country. Here is the code:

 return $code;

Does anybody have an idea how I can change my countries into codes when I have the countrys whole name instead of the codes?

user3011634
  • 13
  • 1
  • 4
  • Maybe you can query their webservice http://www.webservicex.net/country.asmx or this (faster) http://www.geonames.org/export/web-services.html – user2196728 Nov 21 '13 at 01:51
  • Trying to answer your question, if you ask "$country = $row['country'];" you will not have the code...we will need more predision about your DB and your sql queries – user2196728 Nov 21 '13 at 01:59
  • Use this array from http://phpro.org/examples/Country-Array.html and do an `array_flip()` on it to get it the other way around. – jprofitt Nov 21 '13 at 02:02
  • ok seems great, but my question is still here regarding your DB structure and your php queries – user2196728 Nov 21 '13 at 02:04
  • a database call by rows – user3011634 Nov 21 '13 at 02:06
  • useless answer to me, sorry ! Any DB structure ? pK ? SQL query ? I usually call my DBs with a Megaphone, better than "by row"...? You will never "return $code;" if you don't ask for it ! – user2196728 Nov 21 '13 at 02:07

1 Answers1

0

try this kode :

function country_code($country){
    if($country == 'Afganistan')
        $code = 'AF'; 
    //you can add other condition in here like below example
    else if ($country == 'Indonesia')
        $code = 'ID';


return $code; 

}

$code = country_code($row['country']);
echo $code;

Hope this can help you....

EDIT :

Using database

my assumption that you already have a database and table like below..

table name : country
fields : 
id
country
country_code


function country_code($country){
    $sql = "SELECT country_code FROM country WHERE country='$country' LIMIT 1";
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);

    $code = $row['country_code'];


return $code; 

}




$code = country_code($row['country']);
echo $code;
vascowhite
  • 18,120
  • 9
  • 61
  • 77
Hansen
  • 650
  • 1
  • 11
  • 32
  • So you will add a "if" statement for each country ? seems heavy...don't you think ? – user2196728 Nov 21 '13 at 02:01
  • actually u can save the country and country_code into one database.. and use query to get the country code. – Hansen Nov 21 '13 at 02:04
  • he already have a DB ! but i don't know how he stores it and how he queries it...that is what i am asking for... – user2196728 Nov 21 '13 at 02:04
  • who is he??? i think you need make a database yourself.. see my edit.... if no,... you need to use Webservice – Hansen Nov 21 '13 at 02:22
  • "he" represent the guy that posted the issue...thanks for the DB edit, i think you have a crystal ball to read what his (the issue owner) database could be :) – user2196728 Nov 21 '13 at 02:25
  • 2
    [Please, don't use `mysql_*` functions in new code](http://stackoverflow.com/q/12859942). They are no longer maintained and the deprecation process has begun, see the [red box](http://php.net/mysql-connect). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli); [this article](http://php.net/mysqlinfo.api.choosing) will help you decide which. If you choose PDO, [here is a good tutorial](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). – vascowhite Nov 21 '13 at 02:47