require 'city.php';
创新互联是一家集网站建设,宁化企业网站建设,宁化品牌网站建设,网站定制,宁化网站建设报价,网络营销,网络优化,宁化网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
// 连接数据库(PDO)
$pdo=new PDO('mysql:host=localhost;dbname=city','root','root');
$pdo-exec('set names utf8');
$pdo-setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
// 实例化城市类
$c=new city();
$result=$c-mycity();
$city_id = 0;
foreach ($result as $k=$value) {
if(count($value) == count($value,1)){
$sql="insert into city_province (province) value ('{$value['province_name']}')";
$smt=$pdo-prepare($sql);
$smt-execute();
}else{
$sql11="insert into city_province (province) value ('{$value['province_name']}')";
$smt11=$pdo-prepare($sql11);
$smt11-execute();
foreach ($value['city'] as $v=$value1) {
$sql2="insert into city_name (name,province_id) values ('{$value1['city_name']}','{$k}')";
$smt2=$pdo-prepare($sql2);
$smt2-execute();
$city_id += 1;
echo $city_id.'br';
if(!empty($value1['area'])){
foreach ($value1['area'] as $valu1e2) {
$sql3="insert into city_area (area,city_id) values ('{$valu1e2}','{$city_id}')";
$smt3=$pdo-prepare($sql3);
$smt3-execute();
}
}
}
}
}
城市类太大了,发不了。
我是把省、市、地区分别存在三个数据表中(我是新手,刚自己写的)
?php return array (
1 =
array (
'province_name' = '北京市',
'city' =
array (
1 =
array (
'city_name' = '市辖区',
'area' =
array (
1 = '东城区',
2 = '西城区',
3 = '崇文区',
4 = '宣武区',
5 = '朝阳区',
6 = '丰台区',
7 = '石景山区',
8 = '海淀区',
9 = '门头沟区',
10 = '房山区',
11 = '通州区',
12 = '顺义区',
13 = '昌平区',
14 = '大兴区',
15 = '怀柔区',
16 = '平谷区',
),
),
2 =
array (
'city_name' = '北京周边',
'area' =
array (
1 = '密云县',
2 = '延庆县',
),
),
),
),
2 =
array (
'province_name' = '天津市',
'city' =
array (
1 =
array (
'city_name' = '市辖区',
'area' =
array (
1 = '和平区',
2 = '河东区',
3 = '河西区',
4 = '南开区',
5 = '河北区',
6 = '红桥区',
7 = '塘沽区',
8 = '汉沽区',
9 = '大港区',
10 = '东丽区',
11 = '西青区',
12 = '津南区',
13 = '北辰区',
14 = '武清区',
15 = '宝坻区',
),
),
2 =
array (
'city_name' = '天津周边',
'area' =
array (
1 = '宁河县',
2 = '静海县',
3 = '蓟 县',
),
),
),
),
3 =
array (
'province_name' = '河北省',
'city' =
array (
1 =
array (
'city_name' = '石家庄市',
'area' =
array (
1 = '长安区',
2 = '桥东区',
3 = '桥西区',
4 = '新华区',
5 = '井陉矿区',
6 = '裕华区',
7 = '井陉县',
8 = '正定县',
9 = '栾城县',
10 = '行唐县',
11 = '灵寿县',
12 = '高邑县',
13 = '深泽县',
14 = '赞皇县',
15 = '无极县',
16 = '平山县',
17 = '元氏县',
18 = '赵 县',
19 = '辛集市',
20 = '藁城市',
21 = '晋州市',
22 = '新乐市',
23 = '鹿泉市',
),
),
2 =
array (
'city_name' = '唐山市',
'area' =
array (
1 = '路南区',
2 = '路北区',
3 = '古冶区',
4 = '开平区',
5 = '丰南区',
6 = '丰润区',
7 = '滦 县',
8 = '滦南县',
9 = '乐亭县',
10 = '迁西县',
11 = '玉田县',
12 = '唐海县',
13 = '遵化市',
14 = '迁安市',
),
),
3 =
array (
'city_name' = '秦皇岛市',
'area' =
array (
1 = '海港区',
2 = '山海关区',
3 = '北戴河区',
4 = '青龙满族自治县',
5 = '昌黎县',
6 = '抚宁县',
7 = '卢龙县',
),
),
4 =
array (
'city_name' = '邯郸市',
'area' =
array (
1 = '邯山区',
2 = '丛台区',
3 = '复兴区',
4 = '峰峰矿区',
5 = '邯郸县',
6 = '临漳县',
7 = '成安县',
8 = '大名县',
9 = '涉 县',
10 = '磁 县',
11 = '肥乡县',
12 = '永年县',
13 = '邱 县',
14 = '鸡泽县',
15 = '广平县',
16 = '馆陶县',
17 = '魏 县',
18 = '曲周县',
19 = '武安市',
),
),
5 =
array (
'city_name' = '邢台市',
'area' =
array (
1 = '桥东区',
2 = '桥西区',
3 = '邢台县',
4 = '临城县',
基本思想就是:在JS动态创建select控件的option,通过Ajax获取在PHP从SQL数据库获取的省市区信息,代码有点长,但很多都是类似的,例如JS中省、市、区获取方法类似,PHP中通过参数不同执行不同的select语句。
index.html代码:
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
""
html xmlns=""
head
title省市区三级联动/title
META http-equiv=Content-Type content="text/html; charset=gb2312"
script src="scripts/thumbnails.js" type="text/javascript"/script
/head
thumbnails.js代码:
window.onload = getProvince;
function createRequest() {//Ajax于PHP交互需要对象
try {
request = new XMLHttpRequest();//创建一个新的请求对象;
} catch (tryMS) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (otherMS) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = null;
}
}
}
return request;
}
function sech(id) {//省市改变时触发,select的onchange事件
var aa = document.getElementById(id);
if(id=="sheng"){
getCity(aa.value);//这里aa.value为省的id
}
if(id=="shi")
{
getCounty(aa.value);//这里aa.value为市的id
}
}
function getProvince() {//获取所有省
request = createRequest();
if (request == null) {
alert("Unable to create request");
return;
}
var url= "getDetails.php?ID=0";//ID=0时传递至PHP时让其获取所有省
request.open("GET", url, true);
request.onreadystatechange = displayProvince; //设置回调函数
request.send(null); //发送请求
}
function getCity(id){//获取省对应的市
request = createRequest();
if (request == null) {
alert("Unable to create request");
return;
}
var url= "getDetails.php?ID=" + escape(id);
request.open("GET", url, true);
request.onreadystatechange = displayCity;
request.send(null);
}
function getCounty(id){//获取市对应的区
request = createRequest();
if (request == null) {
alert("Unable to create request");
return;
}
var url= "getDetails.php?ID=" + escape(id);
request.open("GET", url, true);
request.onreadystatechange = displayCounty;
request.send(null);
}
function displayProvince() {//将获取的数据动态增加至select
if (request.readyState == 4) {
if (request.status == 200) {
var a=new Array;
var b=request.responseText;//将PHP返回的数据赋值给b
a=b.split(",");//通过","将这一数据保存在数组a中
document.getElementById("sheng").length=1;
var obj=document.getElementById("sheng');
for(i=0;i
obj.options.add(new Option(a[i],i+1)); //动态生成OPTION加到select中,第一个参数为Text,第二个参数为Value值.
}
}
}
function displayCity() {//将获取的数据动态增加至select
if (request.readyState == 4) {
if (request.status == 200) {
var a=new Array;
var b=request.responseText;
a=b.split(",");
document.getElementById("shi").length=1;//重新选择
document.getElementById("xian").length=1;//重新选择
if(document.getElementById("sheng").value!="province"){
var obj=document.getElementById('shi');
for(i=0;i
obj.options.add(new Option(a[i], document.getElementById("sheng").value*100+i+1)); //ocument.getElementById("sheng").value*100+i+1对应的是市的ID。
}
}
}
}
function displayCounty() {//将获取的数据增加至select
if (request.readyState == 4) {
if (request.status == 200) {
var a=new Array;
var b=request.responseText;
a=b.split(",");
document.getElementById("xian").length=1;
if(document.getElementById("sheng").value!="province"document.getElementById("shi").value!="city"){
var obj=document.getElementById('xian');
for(i=0;i
obj.options.add(new Option(a[i],i+1001));
}
}
}
}
getDetails.php代码:
?php
header("Content-Type: text/html; charset=gb2312");
$conn = new COM("ADODB.Connection") or die("Cannot start ADO");
$connstr = "Provider=SQLOLEDB;Persist Security Info=False;User ID=root;Password=123456;Initial Catalog=area;Data Source=localhost";
if($_REQUEST['ID']==0){//获得省列表
$conn-Open($connstr); //建立数据库连接
$sqlstr = "select name from Province"; //设置查询字符串
$rs = $conn-Execute($sqlstr); //执行查询获得结果
$num_cols = $rs-Fields-Count(); //得到数据集列数
$Province=array();
$i=0;
while (!$rs-EOF) {
$Province[$i]=$rs-Fields['name']-Value.",";
$rs-MoveNext();
$i++;
}
foreach($Province as $val)
echo $val;
$conn-Close();
$rs = null;
$conn = null;
}
if($_REQUEST['ID']0$_REQUEST['ID']35){//获得省对应的市列表
$conn-Open($connstr); //建立数据库连接
$sqlstr = "select name from City where cid=".$_REQUEST['ID']; //设置查询字符串
$rs = $conn-Execute($sqlstr); //执行查询获得结果
$num_cols = $rs-Fields-Count(); //得到数据集列数
$City=array();
$i=0;
while (!$rs-EOF) {
$City[$i]=$rs-Fields['name']-Value.",";
$rs-MoveNext();
$i++;
}
foreach($City as $val)
echo $val;
$conn-Close();
$rs = null;
$conn = null;
}
if($_REQUEST['ID']100){//获得省市对应的县列表
$conn-Open($connstr); //建立数据库连接
$sqlstr = "select name from County where cid=".$_REQUEST['ID']; //设置查询字符串
$rs = $conn-Execute($sqlstr); //执行查询获得结果
$num_cols = $rs-Fields-Count(); //得到数据集列数
$County=array();
$i=0;
while (!$rs-EOF) {
$County[$i]=$rs-Fields['name']-Value.",";
$rs-MoveNext();
$i++;
}
foreach($County as $val)
echo $val;
$conn-Close();
$rs = null;
$conn = null;
}
?
数据库设计,表格Province表,City表,County表。
要求:Province表需要id和name,id建议从1至34,例如北京id为1,广东id为2,以此类推;
City表需要id,name和cid,id为cid*100+1,cid为该市的上级,例如深圳的上级为广东省,cid为2的话,深圳的id就是201,以此类推。
County表需要id,name和cid,因为是三级的关系,id可以随意,建议从10001开始自增。cid为所在上级,例如宝安区的cid为201,龙岗区的cid也为201;
截图:
HTML效果:
完成后效果:
有3种方法:
1、手工一行一行的写成php代码
2、使用程序分析图一的数据,写入到数据库里,然后动态查询
3、使用程序分析图一的数据,写入到数据库里,动态生成图二的代码
需要使用到ajax。到网上查下,一堆。代码是固定的。
数据库设置
id,area,areaname
1 210000 辽宁省
2 210100 沈阳市
3 210104 大东区
select name='area1' id='area1' onchange="get_area2(this.value);"
这里读取省的数据
$sql="select area,areaname from area where right(area,4)='0000'";
/select
select name='area2' id='area2 'onchange="get_area(this.value);"
这里根据ajax读取数据,开始的时候是空的
/select
select name='area' id='area'
这里根据ajax读取数据,开始的时候是空的
/select
方法:
1,首先写get_area2 的js 代码,这里就用到ajax读取,这里获取的数据是区域代码的前两位代码(比如:21)
这个在根据这个21的参数,读取表中相关的市,
$sql="select area,areaname from area where left(area,2)='21' and right(area,2)='00'";
这个语句读取出来21的市代码
2,同一读取出来区的代码