網(wǎng)站上有很多jq的三級(jí)聯(lián)動(dòng),主要是城市地區(qū)數(shù)據(jù),那我們?cè)趺传@取到這些數(shù)據(jù)并且保存在我們的數(shù)據(jù)表中,
這是國家統(tǒng)計(jì)局的數(shù)據(jù),下面我們用PHP程序?qū)⑺麄儾杉聛?br />
1.數(shù)據(jù)采集
public function countryaddress() {
$ModelObj = D('Areas');
$baseUrl = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2016/';
//第一級(jí)
$firstDetail = file_get_contents($baseUrl . 'index.html');
$firstPattern = '/<td><a href=\'(\d+).html\'>(.*?)<br\/><\/a><\/td>/i';
preg_match_all($firstPattern, $firstDetail, $firstResult);
$urllist = $firstResult[1];
$namelist = $firstResult[2];
foreach ($namelist as $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)來定
'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 = "/<td><a href=\'" . $urllist[$k] . "\/(\d+?)\.html\'>(\d+?)<\/a><\/td><td><a href=\'" . $urllist[$k] . "\/(\d+?)\.html\'>(.*?)<\/a>/i";
preg_match_all($secondPattern, $secondDetail, $secondResult);
$secondUrlList = $secondResult[1];
$secondCodeList = $secondResult[2];
$secondNameList = $secondResult[4];
foreach ($secondNameList as $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 = '/<td>(?:<a href=\'.*?\'>)*(\d+?)(?:<\/a>)*<\/td><td>(?:<a href=\'.*?\'>)*(.*?)(?:<\/a>)*<\/td>/i';
preg_match_all($thirdPattern, $thirdDetail, $thirdResult);
$thirdCodeList = $thirdResult[1];
$thirdNameList = $thirdResult[2];
foreach ($thirdNameList as $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í)代碼替換
/**
*
*/
public function provincereplace() {
//省級(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 ($provinceCode as $k => $v) {
$ModelObj->where(array('name' => $v['title']))->setField('code', $v['code']);
}
}
3.處理級(jí)別父級(jí)代碼
/**
* 處理級(jí)別父級(jí)代碼
*/
public function levelandparentcode() {
$ModelObj = D('Areas');
$alllist = $ModelObj->select();
foreach ($alllist as $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)單。
<form id="searce">
<div class="screen-li">
<select name="province" id="province">
<option value="">省</option>
<volist name="province" id="item"> <!--循環(huán)省數(shù)據(jù)-->
<option value="{$item.code}">{$item.name}</option>
</volist>
</select>
</div>
<div class="screen-li">
<select name='city' id="city" >
<option value="">市</option>
</select>
</div>
<div class="screen-li">
<select name='area' id="area" >
<option value="">區(qū)/縣</option>
</select>
</div>
<script>
$('#province).change(function() {
id=$(this).val();
get_city(id)
})
function get_city(id) {
url_get_city="{:U('city')}?id="+id
$("#city").load(url_get_city,{n:Math.random()});
}
$('#city).change(function() {
id=$(this).val();
get_area(id)
})
function get_area(id) {
url_get_area="{:U('area')}?id="+id
$("#area").load(url_get_area,{n:Math.random()});
}
</script>
后臺(tái)只要通過id查詢出該級(jí)別下的數(shù)據(jù)就可以
如沒特殊注明,文章均為方維網(wǎng)絡(luò)原創(chuàng),轉(zhuǎn)載請(qǐng)注明來自http://m.oulysa.com/news/6051.html