小项目中存在很多省市区加模糊查询:
以下THINKPHP + JS 构成万能查询代码;方便快捷。
前端:
<select id="s_province" name="pro"></select> <select id="s_city" name="city" ></select> <select id="s_county" name="area"></select> <input type="text" style=" 200px" name = "search" id ="search" value ="{$search}" placeholder="地址综合会员名称搜索"> <input type="button" value ="搜索" onclick = "search('/index.php?s=/Shendou/Member/mList')"> <script class="resources library" src="__STATIC__/area.js" type="text/javascript"></script> <script type="text/javascript">_init_area();</script> <script> $pro = "<?php echo $pro ?>"; $city = "<?php echo $city ?>"; $area = "<?php echo $area ?>"; if($pro != ""){ update($pro,$city,$area); } </script>
效果:
JS 代码
function search(basePath){ var pro = $("#s_province").val(); var city = $("#s_city").val(); var area = $("#s_county").val(); //obj 代表当前选哪个select框框 var parm = ""; var type = ""; if(area != '市、县级市'){ type = "area"; parm = area; }else if(city != '地级市'){ type = "city"; parm = city; }else if(pro != '省份'){ type = "pro"; parm = pro; } var search =$("#search").val(); window.location.href = basePath+"/type/"+type+"/parm/"+parm+"/search/"+search+"/pro/"+pro+"/city/"+city+"/area/"+area+".html"; }
bathPath: 主要是构造提交路径;
后端代码:
$wherestr = " AND 1 = 1 "; $type = $_REQUEST['type']; $parm = $_REQUEST['parm']; $search = $_REQUEST['search']; if($parm){ $wherestr .= "AND `$type` = '$parm'"; } if($search){ $wherestr .= "AND `shopname` like '%$search%'"; } $type = $_REQUEST['type']; $parm = $_REQUEST['parm']; $search = $_REQUEST['search']; if($parm){ $wherestr .= "AND `$type` = '$parm'"; } if($search){ $wherestr .= "AND `shopname` like '%$search%'"; }
维护查询后选中
$pro = $_REQUEST['pro'];
$city = $_REQUEST['city'];
$area = $_REQUEST['area'];
$this->assign('pro',$pro);
$this->assign('city',$city);
$this->assign('area',$area);
$this->assign('search',$search);