<?php
/*
Author: Ahmet M. Okar (hide@address.com)
Package: Cities_of_the_World
File: class_cities.inc
Desc: PHP Class for dynamic creation of countries and cities dropdown lists
with data extracted from a MySQL database.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA.
*/
class cities
{
var $db_handle;
var $db_select;
var $country_query;
var $city_query;
var $caption;
var $trailer;
// Class constructor for making everything ready for data extraction from the database.
function cities($db_host, $db_user, $db_pass, $db_name)
{
$this->db_handle = @mysql_connect($db_host, $db_user, $db_pass);
if (!$this->db_handle) $this->print_error("Database connection failure");
$this->db_select = @mysql_select_db($db_name, $this->db_handle);
if (!$this->db_select) $this->print_error("Database selection failure");
}
// JavaScript to embed within the '<head> </head>' tags of the page displaying the form.
// It disables the 'City' dropdown list if a country different than the one specified is selected.
function java_script($country)
{
echo "<script language=\"JavaScript\" type=\"text/javascript\">\n";
echo "<!--\n";
echo "function WhichCountry(form) {\n";
echo "if(form.country.options[form.country.selectedIndex].value != \"".$country."\") {\n";
echo " eval(form.city.selectedIndex = 0);\n";
echo " form.city.disabled = true;\n";
echo "}\n";
echo "else\n";
echo " form.city.disabled = false;\n";
echo "}\n";
echo "// -->\n";
echo "</script>\n";
}
// Method for populating the 'Country' dropdown SELECT list of the form.
function country_list($db_table)
{
$this->country_query = @mysql_query("SELECT DISTINCT cc, country FROM $db_table");
if (!$this->country_query) $this->print_error("Data extraction failure");
echo "<select name=\"country\" onchange=\"javascript: WhichCountry(this.form);\">\n";
echo "<option value=\"0\">Please Select</option>\n";
while(list ($country_value, $country_label) = mysql_fetch_array($this->country_query))
{
echo "<option value=\"".$country_value."\">".$country_label."</option>\n";
}
echo "</select>\n";
}
// Method to determine whether the field caption will be 'City:' or 'State'.
function caption($country)
{
$this->caption = "City:";
if ($country == "us") $this->caption = "State:";
if ($country == "aq") $this->caption = "Station:";
echo $this->caption;
}
// Method for populating the 'City/State' dropdown SELECT list of the form.
function city_list($db_table, $country)
{
echo "<select name=\"city\">\n";
echo "<option value=\"0\">Please Select</option>\n";
$this->city_query = @mysql_query("SELECT local_id, city, country FROM $db_table WHERE cc = '$country'");
if (!$this->city_query) $this->print_error("Data extraction failure");
while(list ($city_value, $city_label, $country_name) = mysql_fetch_array($this->city_query))
{
echo "<option value=\"".$city_value."\">".$city_label."</option>\n";
$this->trailer = $country_name;
}
echo "</select> for ".$this->trailer."\n";
}
// Method for error handling.
function print_error($str)
{
echo $str." => ".mysql_error()."<br>";
}
}
?>