Skip navigation.

Contridentuallity

Not a fact, a theory!?

PHP - List of countries from MySQL database (output Smarty)

This post will help you to create a MySQL database table which contains the data of all the countries in the world. Once you have that setup, I'll show you how to output these countries in a select box using the Smarty templating engine.

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 :wink:

Change root password and login as root - Linux Ubuntu Dapper 6.06Tribulant.com

Comments

Skulled 7. October 2006, 17:12

I had a look at your countries.sql file, but there seem to be some errors in the data.

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. :smile:

Contrid 8. October 2006, 15:18

Thanks for letting me know!

I obtained the countries from http://www.felgall.com/htmlt19.htm so there was HTML comment inside two of the lines.

All fixed now. :smile:

siranmaharjan 3. March 2009, 13:39

hi! i am a fresher in programming specially in smarty. i dont know how to interact with database in smarty in the case of OOP. some one plz help me by telling how to work with database in the case of OOP.
i will be very thankful to you if you help me.

Write a comment

You must be logged in to write a comment. If you're not a registered member, please sign up.

Download Opera, the fastest and most secure browser
November 2009
S M T W T F S
October 2009December 2009
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30