require?'city.php';
創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、蘭州網(wǎng)絡(luò)推廣、小程序設(shè)計(jì)、蘭州網(wǎng)絡(luò)營(yíng)銷、蘭州企業(yè)策劃、蘭州品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供蘭州建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址:www.cdcxhl.com
//?連接數(shù)據(jù)庫(kù)(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);
//?實(shí)例化城市類
$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();
}
}
}
}
}
城市類太大了,發(fā)不了。
我是把省、市、地區(qū)分別存在三個(gè)數(shù)據(jù)表中(我是新手,剛自己寫的)
?php return array (
1 =
array (
'province_name' = '北京市',
'city' =
array (
1 =
array (
'city_name' = '市轄區(qū)',
'area' =
array (
1 = '東城區(qū)',
2 = '西城區(qū)',
3 = '崇文區(qū)',
4 = '宣武區(qū)',
5 = '朝陽(yáng)區(qū)',
6 = '豐臺(tái)區(qū)',
7 = '石景山區(qū)',
8 = '海淀區(qū)',
9 = '門頭溝區(qū)',
10 = '房山區(qū)',
11 = '通州區(qū)',
12 = '順義區(qū)',
13 = '昌平區(qū)',
14 = '大興區(qū)',
15 = '懷柔區(qū)',
16 = '平谷區(qū)',
),
),
2 =
array (
'city_name' = '北京周邊',
'area' =
array (
1 = '密云縣',
2 = '延慶縣',
),
),
),
),
2 =
array (
'province_name' = '天津市',
'city' =
array (
1 =
array (
'city_name' = '市轄區(qū)',
'area' =
array (
1 = '和平區(qū)',
2 = '河?xùn)|區(qū)',
3 = '河西區(qū)',
4 = '南開區(qū)',
5 = '河北區(qū)',
6 = '紅橋區(qū)',
7 = '塘沽區(qū)',
8 = '漢沽區(qū)',
9 = '大港區(qū)',
10 = '東麗區(qū)',
11 = '西青區(qū)',
12 = '津南區(qū)',
13 = '北辰區(qū)',
14 = '武清區(qū)',
15 = '寶坻區(qū)',
),
),
2 =
array (
'city_name' = '天津周邊',
'area' =
array (
1 = '寧河縣',
2 = '靜??h',
3 = '薊 縣',
),
),
),
),
3 =
array (
'province_name' = '河北省',
'city' =
array (
1 =
array (
'city_name' = '石家莊市',
'area' =
array (
1 = '長(zhǎng)安區(qū)',
2 = '橋東區(qū)',
3 = '橋西區(qū)',
4 = '新華區(qū)',
5 = '井陘礦區(qū)',
6 = '裕華區(qū)',
7 = '井陘縣',
8 = '正定縣',
9 = '欒城縣',
10 = '行唐縣',
11 = '靈壽縣',
12 = '高邑縣',
13 = '深澤縣',
14 = '贊皇縣',
15 = '無(wú)極縣',
16 = '平山縣',
17 = '元氏縣',
18 = '趙 縣',
19 = '辛集市',
20 = '藁城市',
21 = '晉州市',
22 = '新樂市',
23 = '鹿泉市',
),
),
2 =
array (
'city_name' = '唐山市',
'area' =
array (
1 = '路南區(qū)',
2 = '路北區(qū)',
3 = '古冶區(qū)',
4 = '開平區(qū)',
5 = '豐南區(qū)',
6 = '豐潤(rùn)區(qū)',
7 = '灤 縣',
8 = '灤南縣',
9 = '樂亭縣',
10 = '遷西縣',
11 = '玉田縣',
12 = '唐海縣',
13 = '遵化市',
14 = '遷安市',
),
),
3 =
array (
'city_name' = '秦皇島市',
'area' =
array (
1 = '海港區(qū)',
2 = '山海關(guān)區(qū)',
3 = '北戴河區(qū)',
4 = '青龍滿族自治縣',
5 = '昌黎縣',
6 = '撫寧縣',
7 = '盧龍縣',
),
),
4 =
array (
'city_name' = '邯鄲市',
'area' =
array (
1 = '邯山區(qū)',
2 = '叢臺(tái)區(qū)',
3 = '復(fù)興區(qū)',
4 = '峰峰礦區(qū)',
5 = '邯鄲縣',
6 = '臨漳縣',
7 = '成安縣',
8 = '大名縣',
9 = '涉 縣',
10 = '磁 縣',
11 = '肥鄉(xiāng)縣',
12 = '永年縣',
13 = '邱 縣',
14 = '雞澤縣',
15 = '廣平縣',
16 = '館陶縣',
17 = '魏 縣',
18 = '曲周縣',
19 = '武安市',
),
),
5 =
array (
'city_name' = '邢臺(tái)市',
'area' =
array (
1 = '橋東區(qū)',
2 = '橋西區(qū)',
3 = '邢臺(tái)縣',
4 = '臨城縣',
需要使用到ajax。到網(wǎng)上查下,一堆。代碼是固定的。
數(shù)據(jù)庫(kù)設(shè)置
id,area,areaname
1 210000 遼寧省
2 210100 沈陽(yáng)市
3 210104 大東區(qū)
select name='area1' id='area1' onchange="get_area2(this.value);"
這里讀取省的數(shù)據(jù)
$sql="select area,areaname from area where right(area,4)='0000'";
/select
select name='area2' id='area2 'onchange="get_area(this.value);"
這里根據(jù)ajax讀取數(shù)據(jù),開始的時(shí)候是空的
/select
select name='area' id='area'
這里根據(jù)ajax讀取數(shù)據(jù),開始的時(shí)候是空的
/select
方法:
1,首先寫get_area2 的js 代碼,這里就用到ajax讀取,這里獲取的數(shù)據(jù)是區(qū)域代碼的前兩位代碼(比如:21)
這個(gè)在根據(jù)這個(gè)21的參數(shù),讀取表中相關(guān)的市,
$sql="select area,areaname from area where left(area,2)='21' and right(area,2)='00'";
這個(gè)語(yǔ)句讀取出來21的市代碼
2,同一讀取出來區(qū)的代碼