所有分类
  • 所有分类
  • 其它

PHP开发制作三级城市联动效果

网站上有很多jq的三级联动,主要是城市地区数据,那我们怎么获取到这些数据并且保存在我们的数据表中,

统计局数据

这是国家统计局的数据,下面我们用PHP程序将他们采集下来
1.数据采集
  public function countryaddress() {
        $ModelObj = D(‘Areas’);
        $baseUrl = ‘http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2016/’;
        //第一级
        $firstDetail = file_get_contents($baseUrl . ‘index.html’);
        $firstPattern = ‘/

(.*?)
/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, //省级编码暂时默认以排序号来定
                    ‘name’ => iconv(‘GB2312’, ‘UTF-8’, $v),
                    ‘parent_id’ => 0,
                );
                $first_re = $ModelObj->data($adddata)->add();
                if ($first_re) {
                    //第二级
                    $secondDetail = file_get_contents($baseUrl . $urllist[$k] . ‘.html’);
                    $secondPattern = “/
(d+?) (.*?)/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) {
                                //第三级
                                $thirdDetail = file_get_contents($baseUrl . $urllist[$k] . ‘/’ . $secondUrlList[$m] . ‘.html’);
                                $thirdPattern = ‘/
(?:)*(d+?)(?:)* (?:)*(.*?)(?:)*/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.省级代码替换
  /**
     * 
     */
    public function provincereplace() {
        //省级code
        $provinceCode = array(
            array(‘code’ => ‘110000’, ‘title’ => ‘北京市’),
            array(‘code’ => ‘120000’, ‘title’ => ‘天津市’),
            array(‘code’ => ‘130000’, ‘title’ => ‘河北省’),
            array(‘code’ => ‘140000’, ‘title’ => ‘山西省’),
            array(‘code’ => ‘150000’, ‘title’ => ‘内蒙古自治区’),
            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’ => ‘广西壮族自治区’),
            array(‘code’ => ‘460000’, ‘title’ => ‘海南省’),
            array(‘code’ => ‘500000’, ‘title’ => ‘重庆市’),
            array(‘code’ => ‘510000’, ‘title’ => ‘四川省’),
            array(‘code’ => ‘520000’, ‘title’ => ‘贵州省’),
            array(‘code’ => ‘530000’, ‘title’ => ‘云南省’),
            array(‘code’ => ‘540000’, ‘title’ => ‘西藏自治区’),
            array(‘code’ => ‘610000’, ‘title’ => ‘陕西省’),
            array(‘code’ => ‘620000’, ‘title’ => ‘甘肃省’),
            array(‘code’ => ‘630000’, ‘title’ => ‘青海省’),
            array(‘code’ => ‘640000’, ‘title’ => ‘宁夏回族自治区’),
            array(‘code’ => ‘650000’, ‘title’ => ‘新疆维吾尔自治区’),
        );
        $ModelObj = D(‘Areas’);
        foreach ($provinceCode as $k => $v) {
            $ModelObj->where(array(‘name’ => $v[‘title’]))->setField(‘code’, $v[‘code’]);
        }
    }
 
3.处理级别父级代码
  /**
     * 处理级别父级代码
     */
    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. 有了三级的数据做联动就很简单。
 

   

    
   

 

     
 

 
 


 
 
后台只要通过id查询出该级别下的数据就可以

社交账号快速登录