<?php

include('conn.php');

mysql_query("SET character_set_results=utf8");

if(isset($_POST["post_code"]) && !empty($_POST["post_code"])){
	
	$sql="select id, police_station_ban from police_station_list where district_id in (SELECT district_id FROM post_office_list where post_code = '".$_POST['post_code']."') order by police_station_ban asc";
	
	$sql_post_code="SELECT * FROM post_office_list WHERE post_code = '".$_POST['post_code']."'";
	$query_post_code=mysql_query($sql_post_code);
	$row_post_code=mysql_fetch_array($query_post_code);
	
	$query=mysql_query($sql);
	
    if(mysql_num_rows($query)>0){
        echo '<option value="">Select PS</option>';
        while($row=mysql_fetch_array($query)){
			if($row['id']==$row_post_code['police_station_id']){
				echo '<option value="'.$row['id'].'" selected>'.$row['police_station_ban'].'</option>';
			}
			else{
            	echo '<option value="'.$row['id'].'">'.$row['police_station_ban'].'</option>';
			}
        }
    }else{
        echo '<option value="">PS not available</option>';
    }
}

?>