相關(guān)關(guān)鍵詞
關(guān)于我們
最新文章
- PHP中opcode緩存簡(jiǎn)單用法分析
- thinkPHP控制器變量在模板中的顯示方法示例
- PHP move_uploaded_file() 函數(shù)(將上傳的文件移動(dòng)到新位置)
- dirname(__FILE__)的含義和應(yīng)用說明
- thinkPHP5框架實(shí)現(xiàn)分頁(yè)查詢功能的方法示例
- PHP中單雙號(hào)與變量
- PHP獲得當(dāng)日零點(diǎn)時(shí)間戳的方法分析
- Laravel ORM對(duì)Model::find方法進(jìn)行緩存示例詳解
- PHP讀寫文件高并發(fā)處理操作實(shí)例詳解
- 【CLI】利用Curl下載文件實(shí)時(shí)進(jìn)度條顯示的實(shí)現(xiàn)
php+mysql+ajax實(shí)現(xiàn)單表多字段多關(guān)鍵詞查詢的方法
本文實(shí)例講述了php+mysql+ajax實(shí)現(xiàn)單表多字段多關(guān)鍵詞查詢的方法。分享給大家供大家參考,具體如下:
單表多字段查詢?cè)谝恍┥晕?fù)雜一點(diǎn)的查詢中十分有用。這里主要利用MySQL數(shù)據(jù)庫(kù)中的concat函數(shù)實(shí)現(xiàn)單表多字段多關(guān)鍵詞查詢。并且顯示查詢結(jié)果的表格可根據(jù)所選數(shù)據(jù)表動(dòng)態(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();//查詢按鈕獲得焦點(diǎn) $("#query").click();//調(diào)用點(diǎn)擊提交按鈕 } }); //點(diǎn)擊查詢按鈕 $("#query").click(function(){ //獲取用戶選擇的數(shù)據(jù)表和查詢關(guān)鍵詞 var queryType=$("#queryType").val().trim(); var queryKeywords=$("#queryKeywords").val().trim(); //對(duì)查詢關(guān)系詞的內(nèi)容進(jìn)行限制,比如只能輸入漢字、字母、數(shù)字和空格。 var reg=/^[ a-zA-Z0-9\u4e00-\u9fa5]+$/; if(reg.test(queryKeywords)==false) { $("#queryTip").text("您輸入的關(guān)鍵詞有部分內(nèi)容不符合要求,只能輸入漢字、字母、數(shù)字和空格。"); } //如果內(nèi)容檢測(cè)通過,開始提交查詢 else { if(queryType!="" && queryKeywords!="") { $("#queryTip").text("正在為您檢索,請(qǐng)稍候……"); //使用ajax訪問php頁(yè)面進(jìn)行查詢 $.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é)號(hào),學(xué)位類型,導(dǎo)師,手機(jī),房間"; else if($("#queryType").val()=="papers") fieldsString="作者,標(biāo)題,期刊/會(huì)議,卷期頁(yè),級(jí)別,狀態(tài)"; else if($("#queryType").val()=="softwares") fieldsString="作者,標(biāo)題,登記號(hào),日期"; else if($("#queryType").val()=="patents") fieldsString="作者,標(biāo)題,受理日期,受理號(hào),授權(quán)日期,授權(quán)號(hào)"; //調(diào)用函數(shù)創(chuàng)建表格 createTable("queryResultList",data,fieldsString,"queryResultPager"); $("#queryKeywords").focus();// 關(guān)鍵詞文本框繼續(xù)獲得焦點(diǎn),以方便用戶繼續(xù)輸入內(nèi)容 } } }); } else { $("#queryTip").text("請(qǐng)先選擇查詢類型,并輸入關(guān)鍵詞,然后點(diǎn)擊查詢按鈕。"); $("#queryKeywords").focus(); } } }) function createTable(tableHolder,data,fieldsString,pagerHolder) //自動(dòng)創(chuàng)建結(jié)果表格的函數(shù) /* 參數(shù)含義: tableHolder:顯示結(jié)果表格的父元素 data:JSON格式的查詢結(jié)果 fieldsString:字段名字符串 pagerHolder:顯示分頁(yè)導(dǎo)航的元素 * */ { fieldsArr=fieldsString.split(",");//對(duì)字段名字符串進(jìn)行分割 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; //臨時(shí)循環(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++; } //分頁(yè)導(dǎo)航,這里利用jPages插件 $("#"+pagerHolder).jPages({ containerID : tableHolder+" tbody", //存放表格的窗口標(biāo)簽ID previous : "前一頁(yè)", //指示首頁(yè)的按鈕 next : "下一頁(yè)",//指示尾頁(yè)的按鈕 perPage : 10,//每頁(yè)顯示表格的行數(shù) delay : 0 //分頁(yè)時(shí)動(dòng)畫持續(xù)時(shí)間,0表示無(wú)動(dòng)畫 }); //設(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="">請(qǐng)選擇數(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é)果分頁(yè)導(dǎo)航的容器--> </body> </html>