PHP開(kāi)發(fā)制作三級(jí)城市聯(lián)動(dòng)效果

2023-02-16    分類(lèi): 網(wǎng)站建設(shè)

網(wǎng)站上有很多jq的三級(jí)聯(lián)動(dòng),主要是城市地區(qū)數(shù)據(jù),那我們?cè)趺传@取到這些數(shù)據(jù)并且保存在我們的數(shù)據(jù)表中,
這是國(guó)家統(tǒng)計(jì)局的數(shù)據(jù),下面我們用PHP程序?qū)⑺麄儾杉聛?lái)
1.數(shù)據(jù)采集
publicfunctioncountryaddress(){
$ModelObj=D('Areas');
$baseUrl='http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2016/';
//第一級(jí)
$firstDetail=file_get_contents($baseUrl.'index.html');
$firstPattern='/(.*?)<\/a><\/td>/i';
preg_match_all($firstPattern,$firstDetail,$firstResult);
$urllist=$firstResult[1];
$namelist=$firstResult[2];
foreach($namelistas$k=>$v){
$iscun=$ModelObj->where(array('name'=>iconv('GB2312','UTF-8',$v)))->find();
if(!$iscun){
$adddata=array(
'code'=>$k+1,//省級(jí)編碼暫時(shí)默認(rèn)以排序號(hào)來(lái)定
'name'=>iconv('GB2312','UTF-8',$v),
'parent_id'=>0,
);
$first_re=$ModelObj->data($adddata)->add();
if($first_re){
//第二級(jí)
$secondDetail=file_get_contents($baseUrl.$urllist[$k].'.html');
$secondPattern="/(\d+?)<\/a><\/td>(.*?)<\/a>/i";
preg_match_all($secondPattern,$secondDetail,$secondResult);
$secondUrlList=$secondResult[1];
$secondCodeList=$secondResult[2];
$secondNameList=$secondResult[4];
foreach($secondNameListas$m=>$n){
$iscun=$ModelObj->where(array('name'=>iconv('GB2312','UTF-8',$n),'parent_id'=>$first_re))->find();
if(!$iscun){
$adddata=array(
'code'=>$secondCodeList[$m],
'name'=>iconv('GB2312','UTF-8',$n),
'parent_id'=>$first_re,
);
$second_re=$ModelObj->add($adddata);
if($second_re){
//第三級(jí)
$thirdDetail=file_get_contents($baseUrl.$urllist[$k].'/'.$secondUrlList[$m].'.html');
$thirdPattern='/(?:)*(\d+?)(?:<\/a>)*<\/td>(?:)*(.*?)(?:<\/a>)*<\/td>/i';
preg_match_all($thirdPattern,$thirdDetail,$thirdResult);
$thirdCodeList=$thirdResult[1];
$thirdNameList=$thirdResult[2];
foreach($thirdNameListas$p=>$q){
$iscun=$ModelObj->where(array('name'=>iconv('GB2312','UTF-8',$q),'parent_id'=>$second_re))->find();
if(!$iscun){
$adddata=array(
'code'=>$thirdCodeList[$p],
'name'=>iconv('GB2312','UTF-8',$q),
'parent_id'=>$second_re,
);
$third_re=$ModelObj->add($adddata);
}
}
}
}
}
}
}
}
}
2.省級(jí)代碼替換
/**
*
*/
publicfunctionprovincereplace(){
//省級(jí)code
$provinceCode=array(
array('code'=>'110000','title'=>'北京市'),
array('code'=>'120000','title'=>'天津市'),
array('code'=>'130000','title'=>'河北省'),
array('code'=>'140000','title'=>'山西省'),
array('code'=>'150000','title'=>'內(nèi)蒙古自治區(qū)'),
array('code'=>'210000','title'=>'遼寧省'),
array('code'=>'220000','title'=>'吉林省'),
array('code'=>'230000','title'=>'黑龍江省'),
array('code'=>'310000','title'=>'上海市'),
array('code'=>'320000','title'=>'江蘇省'),
array('code'=>'330000','title'=>'浙江省'),
array('code'=>'340000','title'=>'安徽省'),
array('code'=>'350000','title'=>'福建省'),
array('code'=>'360000','title'=>'江西省'),
array('code'=>'370000','title'=>'山東省'),
array('code'=>'410000','title'=>'河南省'),
array('code'=>'420000','title'=>'湖北省'),
array('code'=>'430000','title'=>'湖南省'),
array('code'=>'440000','title'=>'廣東省'),
array('code'=>'450000','title'=>'廣西壯族自治區(qū)'),
array('code'=>'460000','title'=>'海南省'),
array('code'=>'500000','title'=>'重慶市'),
array('code'=>'510000','title'=>'四川省'),
array('code'=>'520000','title'=>'貴州省'),
array('code'=>'530000','title'=>'云南省'),
array('code'=>'540000','title'=>'西藏自治區(qū)'),
array('code'=>'610000','title'=>'陜西省'),
array('code'=>'620000','title'=>'甘肅省'),
array('code'=>'630000','title'=>'青海省'),
array('code'=>'640000','title'=>'寧夏回族自治區(qū)'),
array('code'=>'650000','title'=>'新疆維吾爾自治區(qū)'),
);
$ModelObj=D('Areas');
foreach($provinceCodeas$k=>$v){
$ModelObj->where(array('name'=>$v['title']))->setField('code',$v['code']);
}
}
3.處理級(jí)別父級(jí)代碼
/**
*處理級(jí)別父級(jí)代碼
*/
publicfunctionlevelandparentcode(){
$ModelObj=D('Areas');
$alllist=$ModelObj->select();
foreach($alllistas$k=>$v){
if(!$v['parent_id']){
$data=array(
'level'=>1,
'parent_code'=>'0',
);
}else{
$parent=$ModelObj->where(array('id'=>$v['parent_id']))->find();
if(!$parent['parent_id']){
$data=array(
'level'=>2,
'parent_code'=>$parent['code'],
);
}else{
$data=array(
'level'=>3,
'parent_code'=>$parent['code'],
);
}
}
$re=$ModelObj->where(array('id'=>$v['id']))->save($data);
}
}
4.有了三級(jí)的數(shù)據(jù)做聯(lián)動(dòng)就很簡(jiǎn)單。





{$item.name}










區(qū)/縣


標(biāo)題名稱(chēng):PHP開(kāi)發(fā)制作三級(jí)城市聯(lián)動(dòng)效果
URL地址:http://muchs.cn/news/237475.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、軟件開(kāi)發(fā)、響應(yīng)式網(wǎng)站網(wǎng)站導(dǎo)航、網(wǎng)站策劃、外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

猜你還喜歡下面的內(nèi)容

搜索引擎優(yōu)化

網(wǎng)站建設(shè)知識(shí)

行業(yè)建站