php+mysql+ajax實現(xiàn)單表多字段多關(guān)鍵詞查詢的方法
本文實例講述了php+mysql+ajax實現(xiàn)單表多字段多關(guān)鍵詞查詢的方法。分享給大家供大家參考,具體如下:
單表多字段查詢在一些稍微復(fù)雜一點的查詢中十分有用。這里主要利用MySQL數(shù)據(jù)庫中的concat函數(shù)實現(xiàn)單表多字段多關(guān)鍵詞查詢。并且顯示查詢結(jié)果的表格可根據(jù)所選數(shù)據(jù)表動態(tài)生成。
html代碼:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="js/jquery-1.9.1.min.js" type="text/javascript" charset="utf-8"></script> <script src="js/jPages.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> $(function(){ //回車提交查詢 $('#queryBox').keydown(function(e){ if(e.keyCode == 13) { $("#query").focus();//查詢按鈕獲得焦點 $("#query").click();//調(diào)用點擊提交按鈕 } }); //點擊查詢按鈕 $("#query").click(function(){ //獲取用戶選擇的數(shù)據(jù)表和查詢關(guān)鍵詞 var queryType=$("#queryType").val().trim(); var queryKeywords=$("#queryKeywords").val().trim(); //對查詢關(guān)系詞的內(nèi)容進行限制,比如只能輸入漢字、字母、數(shù)字和空格。 var reg=/^[ a-zA-Z0-9\u4e00-\u9fa5]+$/; if(reg.test(queryKeywords)==false) { $("#queryTip").text("您輸入的關(guān)鍵詞有部分內(nèi)容不符合要求,只能輸入漢字、字母、數(shù)字和空格。"); } //如果內(nèi)容檢測通過,開始提交查詢 else { if(queryType!="" && queryKeywords!="") { $("#queryTip").text("正在為您檢索,請稍候……"); //使用ajax訪問php頁面進行查詢 $.ajax({ type:"post", url:"queryInIndex.php", async:true, data:{queryType:$("#queryType").val(),queryKeywords:$("#queryKeywords").val()}, dataType:"json", success:function(data){ if(data=="no") { $("#queryTip").text("沒有找到您要查詢的信息。"); } else { $("#queryTip").text("為您找到 "+data.length+" 條信息。"); //預(yù)設(shè)結(jié)果表格字符名 if($("#queryType").val()=="users") fieldsString="郵箱,姓名,英文名,學(xué)號,學(xué)位類型,導(dǎo)師,手機,房間"; else if($("#queryType").val()=="papers") fieldsString="作者,標(biāo)題,期刊/會議,卷期頁,級別,狀態(tài)"; else if($("#queryType").val()=="softwares") fieldsString="作者,標(biāo)題,登記號,日期"; else if($("#queryType").val()=="patents") fieldsString="作者,標(biāo)題,受理日期,受理號,授權(quán)日期,授權(quán)號"; //調(diào)用函數(shù)創(chuàng)建表格 createTable("queryResultList",data,fieldsString,"queryResultPager"); $("#queryKeywords").focus();// 關(guān)鍵詞文本框繼續(xù)獲得焦點,以方便用戶繼續(xù)輸入內(nèi)容 } } }); } else { $("#queryTip").text("請先選擇查詢類型,并輸入關(guān)鍵詞,然后點擊查詢按鈕。"); $("#queryKeywords").focus(); } } }) function createTable(tableHolder,data,fieldsString,pagerHolder) //自動創(chuàng)建結(jié)果表格的函數(shù) /* 參數(shù)含義: tableHolder:顯示結(jié)果表格的父元素 data:JSON格式的查詢結(jié)果 fieldsString:字段名字符串 pagerHolder:顯示分頁導(dǎo)航的元素 * */ { fieldsArr=fieldsString.split(",");//對字段名字符串進行分割 var columnNum=fieldsArr.length; //獲取列數(shù) var rowNum=data.length;//獲取行數(shù) $('#'+tableHolder).html("");//清除現(xiàn)有表格 $('#'+tableHolder).append("<table border='1'><thead><tr></tr></thead><tbody></tbody></table>"); //設(shè)置表格結(jié)構(gòu) var i=0; //臨時循環(huán)變量 while(i<columnNum) //加載字段值 { $('#'+tableHolder+" thead tr").append("<th>"+fieldsArr[i]+"</th>"); i++; } i=0; while(i<rowNum)//加載數(shù)據(jù) { var j=0; $('#'+tableHolder+" tbody").append("<tr>") while(j<columnNum) { $('#'+tableHolder+" tbody tr:last").append("<td>"+data[i][j]+"</td>"); j++; } $('#'+tableHolder+" tbody").append("</tr>") i++; } //分頁導(dǎo)航,這里利用jPages插件 $("#"+pagerHolder).jPages({ containerID : tableHolder+" tbody", //存放表格的窗口標(biāo)簽ID previous : "前一頁", //指示首頁的按鈕 next : "下一頁",//指示尾頁的按鈕 perPage : 10,//每頁顯示表格的行數(shù) delay : 0 //分頁時動畫持續(xù)時間,0表示無動畫 }); //設(shè)置結(jié)果表格的隔行變色 $('#'+tableHolder+" tr:odd").css("background-color","#fff"); $('#'+tableHolder+" tr:even").css("background-color","#efefef"); } }) </script> </head> <body> <div id="queryBox" style="text-align: center; padding-top: 10px;padding-bottom: 10px; width: 100%;"> <span> <select id="queryType" style="height: 30px;"><!--選擇數(shù)據(jù)表的下拉菜單--> <option selected="selected" value="">請選擇數(shù)據(jù)表</option> <option value="users">學(xué)生信息</option> <option value="papers">論文</option> <option value="softwares">軟著</option> <option value="patents">專利</option> </select> </span> <span> <input type="text" id='queryKeywords' style="width: 200px; height: 25px;"/><!--輸入查詢關(guān)鍵詞的文本框--> <input type="button" id="query" style="width: 30px; height: 30px; cursor: pointer;" /> <!--提交查詢按鈕--> </span> <div id="queryTip" class="tip">本功能支持以空格為分割標(biāo)記的多詞查詢。</div><!--信息提示框--> </div> <div id="queryResultList"></div><!--用于顯示查詢結(jié)果--> <div id="queryResultPager" style="text-align: center; margin-top: 10px;"></div><!--結(jié)果分頁導(dǎo)航的容器--> </body> </html>
queryInIndex.php:
<?php include_once("../phpFiles/connMysql.php");//包含連接數(shù)據(jù)庫的代碼 $queryType=$_POST['queryType'];//目標(biāo)數(shù)據(jù)表 $queryKeywords=$_POST['queryKeywords']; //查詢詞 $queryKeywords = preg_replace('#\s+#', ' ',$queryKeywords);//將字符串中多個連續(xù)空格替換為一個空格 $keywords=explode(" ",$queryKeywords); //查詢詞按空格分割 //根據(jù)不同的數(shù)據(jù)表生成不同的查詢語句 if($queryType=="users") { $sqlsmt="select email,truename,enName,stuid,degree,supervisorname,tel,room from $queryType where "; //查詢語句的初始值 //利用循環(huán) 生成對每個關(guān)鍵詞的查詢語句 foreach ($keywords as $keyword) { $sqlsmt.="concat(email,truename,enName,stuid,degree,supervisorname,tel,room) like '%$keyword%' or ";//本句是關(guān)鍵,利用concat函數(shù)實現(xiàn)從多個字段查詢單個關(guān)鍵詞,并用' or '關(guān)鍵詞把每個關(guān)鍵詞的concat結(jié)果合并 } $sqlsmt=substr($sqlsmt,0,strlen($sqlsmt)-3); //去掉查詢字符串尾部的' or ' $sqlsmt.=" order by degree asc, stuid desc"; //追加排序子句 } else if($queryType=="papers") { $sqlsmt="select authors,title,jorc,vap,level,status from $queryType where "; foreach ($keywords as $keyword) { $sqlsmt.="concat(authors,title,jorc,vap,level,status) like '%$keyword%' or "; } $sqlsmt=substr($sqlsmt,0,strlen($sqlsmt)-3); $sqlsmt.=" order by level desc, vap desc"; } else if($queryType=="softwares") { $sqlsmt="select authors,title,num,date from $queryType where "; foreach ($keywords as $keyword) { $sqlsmt.="concat(authors,title,num,date) like '%$keyword%' or "; } $sqlsmt=substr($sqlsmt,0,strlen($sqlsmt)-3); $sqlsmt.=" order by date desc"; } else if($queryType=="patents") { $sqlsmt="select authors,title,acceptDate,acceptNum,authorizeDate,authorizeNum from $queryType where "; foreach ($keywords as $keyword) { $sqlsmt.="concat(authors,title,acceptDate,acceptNum,authorizeDate,authorizeNum) like '%$keyword%' or "; } $sqlsmt=substr($sqlsmt,0,strlen($sqlsmt)-3); $sqlsmt.=" order by acceptDate desc"; } $myrs=mysql_query($sqlsmt);//執(zhí)行查詢 if(mysql_num_rows($myrs)==0)//如果結(jié)果為空 返回'no' { echo(json_encode("no")); } else //否則返回json格式的結(jié)果 { while($row=mysql_fetch_array($myrs)) { $temp[]=$row; } echo(json_encode($temp)); } ?>
運行結(jié)果截圖:
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP+ajax技巧與應(yīng)用小結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
php中批量刪除Mysql中相同前綴的數(shù)據(jù)表的代碼
Mysql如何批量刪除相同前綴的數(shù)據(jù)表,原理就是讀取數(shù)據(jù)中的所有表,查找class_開頭的表,如果開頭是這個,就刪除。2011-07-07Can''t create/write to file ''C:\WINDOWS\TEMP\...MYSQL報錯解決方法
Can't create/write to file 'C:\WINDOWS\TEMP\...MYSQL報錯解決方法,參考下面的方法即可。2011-06-06PHP封裝XML和JSON格式數(shù)據(jù)接口操作示例
這篇文章主要介紹了PHP封裝XML和JSON格式數(shù)據(jù)接口操作,結(jié)合實例形式分析了php針對xml與json格式數(shù)據(jù)接口封裝相關(guān)操作技巧,需要的朋友可以參考下2019-03-03PHP用mysql數(shù)據(jù)庫存儲session的代碼
大部分使用php的人一旦應(yīng)用到session都會使用cookie。 cookie雖好可是它也會給我們帶來一些隱患的。2010-03-03