PHP - List of countries from MySQL database (output Smarty)
Tuesday, 3. October 2006, 00:24:51
I know that many people don't have access to a straight forward SQL file containing the table structure and all the data for the countries of the world, so many of you will appreciate this resource very much.
First off...
You'll see an attachment to this post with the name 'countries.zip' which contains a simple SQL file to create the database structure and insert the appropriate values/data into the table. The SQL file inside the archive is named 'countries.sql'. You'll need to open the file with NotePad, then copy and paste the contents into the SQL query box inside phpMyAdmin, mysqladmin, or whatever you are using to manage your databases.
countries.zip
Once you have that setup...
In order to output a select box containing all the countries, you'll need two files. The first file is the '.php' file which communicates with the database and assigns the Smarty vars. The second file is simply the Smarty '.tpl' file which actually outputs the assigned data from the '.php' file.
countries.php
<?php
################################################
### Database Connection
##
#
// you'll need to edit some of these values accordingly
$host = "localhost";
$user = "username";
$pass = "password";
$database = "countries";
mysql_connect($host, $user, $pass);
mysql_select_db($database) or die(mysql_error());
#
##
###
################################################
################################################
### Main data
##
#
//query the database table 'countries'
$query = "SELECT * FROM countries";
$result = mysql_query($query) or die(mysql_error());
$res = mysql_fetch_array($result);
//create two empty arrays to hold the data
$ids = array();
$values = array();
$i = 0;
while($res = mysql_fetch_array($result))
{
$id = $res['id'];
$value = $res['value'];
$tmp = $i++;
$ids[$tmp] = $id;
$values[$tmp] = $value;
};
$smarty -> assign('country_id', $ids);
$smarty -> assign('country_value', $values);
$smarty -> display('countries.tpl');
#
##
###
################################################
?>
Sorry...gotta run...will finish up later
















Skulled # 7. October 2006, 17:12
There is some weird copyright notices as part of the country names, which I am guessing shouldn't be there.
Take a look.
If you like I can provide you with a nicer countries sql file.
Contrid # 8. October 2006, 15:18
I obtained the countries from http://www.felgall.com/htmlt19.htm so there was HTML comment inside two of the lines.
All fixed now.
siranmaharjan # 3. March 2009, 13:39
i will be very thankful to you if you help me.