本文共 1245 字,大约阅读时间需要 4 分钟。
为了减轻数据库压力,采用逐层异步请求+缓存方式来优化性能。
前端采用字符串拼接+字符串模板方式逐步请求。
源码包括全国省市区数据库+基于tp的控制器+前端页面
github托管地址:
public function index(){ //$this->show('thinkphp'); $db=M('provinces'); $result=S('result'); if(!$result){ $result=$db->select(); //p($result); S('result',$result,30); } $this->assign('province',$result); $this->display(); } public function getCity(){ $proID=I('provinceid'); if(IS_AJAX){ ///p($proID); $db=M('cities'); $where=array('provinceid'=>$proID); $city=$db->where($where)->select(); //p($city); /** * 返回类型有三种情况 * 1.北上津 等直辖市 返回市辖区 * 2.港澳台 返回空 * 3.正常的地级市 */ if($city){ $this->ajaxReturn($city); } }else{ $this->error('非法请求'); } } public function getArea(){ $citID=I('cityid'); if(IS_AJAX){ //p($citID); $db=M('areas'); $where=array('cityid'=>$citID); $area=$db->where($where)->select(); if($area){ $this->ajaxReturn($area); } }else{ $this->error('非法请求'); } }
演示效果:
三级联动的地区 中国省市区三级联动
转载地址:http://zeboi.baihongyu.com/