php ajax实时输入自动搜索匹配程序代码
本文章来给大家介绍php ajax实输入自动搜索匹配程序代码,希望此款程序对大家所有帮助了,第一种方法利用了jquery autocomplete,第二种使用了jquery ajax,其实两个都差不多.
最简单的办法我们就是直接使用jquery autocomplete方法了,如下在实例.
html中,代码如下:
- <script src="/js/v2/jquery.input_complete.js" type="text/javascript" ></script>
- <link rel="stylesheet" href="/css/v2/jquery.input_complete.css" type="text/css" />
- <script type="text/javascript">
- $(function(){
- $("#abcc").bigAutocomplete({width:176,data:[{title:"九龙仓世纪华府",result:"5976898"},{title:"九龙仓君玺",result:"5976896"},{title:"舟山朱家尖东沙度假村",result:"5976895"},{title:"广厦天都城爵士花园",result:"5976893"},{title:"绿城玉园",result:"5976892"},{title:"江南铭楼",result:"5976890"},{title:"世茂江滨花园",result:"14869"}],callback:function(data){
- $("#s_loupan").val(data.result);
-
- }});
-
- })
- </script>
- <input type="text" id="abcc" class="sel_quy" autocomplete="off" />
本文目的是让 某个标题 带有 The 的不按字母t首字母搜索,而是按照第二个文字的首字母搜索.
比如 The Orbolg,按T的时候不显示,按O的时候才显示,下面是实现代码,我自己都晕晕的,测试是没有问题,代码如下:
- public function gwquery(){
- if($_POST){
- $zm = $_POST['data'];
- $wzm = $zm."%";
- $b = M('Wpindexgw');
- if($zm!="t"){
- $list1 = $b->where("gwstatus=1 and gwname like '%$zm%'")->order("time desc")->select();
- $narr =array();
- foreach( $list1 as $key => $val){
- $ct2 = explode(" ",$val['gwname']);
- $szm=strtolower(substr($ct2[1],0,1));
- if($ct2[0]=="The" && $szm==$zm){
- $narr[] = $val;
- }
- }
- }else{
- $list1 = $b->where("gwstatus=1 and gwname like '$wzm'")->order("time desc")->select();
- foreach( $list1 as $key => $val){
- $ct2 = explode(" ",$val['gwname']);
- if($ct2[0]=="The"){
- $unnarr[] = $key;
- }
-
- }
-
- }
- $list = $b->where("gwstatus=1 and gwname like '$wzm'")->order("time desc")->select();
- if($zm=="t"){
- foreach($unnarr as $uval){
- unset($list[$uval]);
- }
- }else{
- if(!emptyempty($narr) && !emptyempty($list)){
- $list = array_merge($narr,$list);
- }else if(emptyempty($list)){
- $list=array();
- $list = array_merge($narr,$list);
- }
- }
- if($list){
- $this->ajaxReturn($list,'success',1);
- }else{
- $this->error("Bestiary not found.");
- }
- }
-
- }
相比之前的代码,相差太多,代码如下:
- public function gwquery1(){
- if($_POST){
- $zm = $_POST['data'];
- $wzm = $zm."%";
- $b = M('Wpindexgw');
- $list = $b->where("gwstatus=1 and gwname like '$wzm'")->order("time desc")->select();
-
- if($list){
- $this->ajaxReturn($list,'success',1);
- }else{
- $this->error("Bestiary not found.");
- }
- }
-
- }