//查找省 $province = DB::select("select * from region WHERE `code` LIKE '%0000'");
foreach ($provinceas$key=>$val) {
//省的识别码,如广东省,为 44 开头 $provinceCode = substr($val->code,0,2); $areaInProvinces = DB::select("select * from region WHERE `code` LIKE '{$provinceCode}%' AND `code` NOT LIKE '%0000' ");
//如果省份内包含市/区 if ($areaInProvinces) foreach ($areaInProvincesas$areaInProvinceKey => $areaInProvince) { //后3位为 0 if ( substr($areaInProvince->code,-3) == '000' ) { //先更新所属 DB::update("update region set pid = '".$val->id."' where id = '".$areaInProvince->id."'");
$code = substr($areaInProvince->code,0,3); DB::update("update region set pid = '{$areaInProvince->id}' WHERE `code` LIKE '{$code}%' AND `code` NOT LIKE '%000' "); continue; }
//后2位为 0 if ( substr($areaInProvince->code,-2) == '00' ) { //先更新所属 DB::update("update region set pid = '".$val->id."' where id = '".$areaInProvince->id."'");
$code = substr($areaInProvince->code,0,4); DB::update("update region set pid = '{$areaInProvince->id}' WHERE `code` LIKE '{$code}%' AND `code` NOT LIKE '%00' "); continue; }