Location: PHPKode > scripts > Free PHP Ajax Yahoo Weather API > free-php-ajax-yahoo-weather-api/stateget.php
<?php
/*
This script was downloaded at:
LightPHPScripts.com
Please support us by visiting
out website and letting people
know of it.
Produced under: LGPL
*/

$zip_code = $_GET['zip_code'];

if($USE_DATA){

	$dbc = mysql_connect ($DB_HOST, $DB_USER, $DB_PASSWORD) or $error = mysql_error();
	mysql_select_db($DB_NAME,$dbc) or $error = mysql_error();
	mysql_query("SET NAMES `utf8`",$dbc);
	
	// If zipcode exist, assign statename/code
	if($zip_code != NULL && is_numeric($zip_code)){
		$get_info = mysql_fetch_assoc(mysql_query("SELECT * FROM `city_list` WHERE `zip_code`='$zip_code'"));
		$_GET['state'] = $get_info['state_name'];
		$cityname = $get_info['city_name'];
		$state_code = $get_info['state_code'];
	}
	
	// Get ready state list
	$get_list = mysql_query("SELECT `state_name` FROM `city_list` GROUP BY `state_name` ASC");
	while($each = mysql_fetch_assoc($get_list)){
		if($_GET['state'] == $each['state_name']){$sel= ' selected';}else{$sel=NULL;}
		$state_list_form .= '<option value="'.$each['state_name'].'"'.$sel.'>'.$each['state_name'].'</option>';
	}
	
	// Print city list
	if($_GET['state'] != NULL){
		$get_city = mysql_query("SELECT * FROM `city_list` WHERE `state_name`='$_GET[state]'");
		while($each = mysql_fetch_assoc($get_city)){
			if($zip_code == $each['zip_code']){
			$sel= ' selected'; 
			}else{$sel=NULL;}
				$city_list_form .= '<option value="'.$each['zip_code'].'"'.$sel.'>'.$each['city_name'].' ['.$each['zip_code'].']</option>';
		}
	}

	mysql_close($dbc);
} else {
	// If zipcode exist, assign statename/code
	if($zip_code != NULL && is_numeric($zip_code)){
		foreach($state_list as $state_name => $variable){
			foreach($state_list[$state_name] as $city_info){
				if($city_info[2] == $zip_code){
					$_GET['state'] = $state_name;
					$cityname = $city_info[0];
					$state_code = $city_info[1];
					break;
				}
			}
			if($city_info[2] == $zip_code){
				break;
			}
		}
	}
	
	// Get ready state list
	foreach($state_list as $this_state => $variable){
		if($_GET['state'] == $this_state){$sel= ' selected';}else{$sel=NULL;}
		$state_list_form .= '<option value="'.$this_state.'"'.$sel.'>'.$this_state.'</option>';
	}
	
	// Print city list
	if($_GET['state']){
		foreach($state_list[$_GET['state']] as $city_info){
			if($zip_code == $city_info[2]){
			$sel= ' selected'; 
				}else{$sel=NULL;}
			$city_list_form .= '<option value="'.$city_info[2].'"'.$sel.'>'.$city_info[0].' ['.$city_info[2].']</option>';
		}
	}
}	
?>
Return current item: Free PHP Ajax Yahoo Weather API