《PHP實(shí)戰(zhàn):php+mysql+ajax實(shí)現(xiàn)單表多字段多關(guān)鍵詞查詢的方法》要點(diǎn):
本文介紹了PHP實(shí)戰(zhàn):php+mysql+ajax實(shí)現(xiàn)單表多字段多關(guān)鍵詞查詢的方法,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
PHP實(shí)例本文實(shí)例講述了php+mysql+ajax實(shí)現(xiàn)單表多字段多關(guān)鍵詞查詢的方法.分享給大家供大家參考,具體如下:
PHP實(shí)例單表多字段查詢?cè)谝恍┥晕?fù)雜一點(diǎn)的查詢中十分有用.這里主要利用MySQL數(shù)據(jù)庫中的concat函數(shù)實(shí)現(xiàn)單表多字段多關(guān)鍵詞查詢.并且顯示查詢結(jié)果的表格可根據(jù)所選數(shù)據(jù)表動(dòng)態(tài)生成.
PHP實(shí)例html代碼:
PHP實(shí)例
<!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)容檢測通過,開始提交查詢
else
{
if(queryType!="" && queryKeywords!="")
{
$("#queryTip").text("正在為您檢索,請(qǐng)稍候……");
//使用ajax訪問php頁面進(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ì)議,卷期頁,級(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:顯示分頁導(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++;
}
//分頁導(dǎo)航,這里利用jPages插件
$("#"+pagerHolder).jPages({
containerID : tableHolder+" tbody", //存放表格的窗口標(biāo)簽ID
previous : "前一頁", //指示首頁的按鈕
next : "下一頁",//指示尾頁的按鈕
perPage : 10,//每頁顯示表格的行數(shù)
delay : 0 //分頁時(shí)動(dòng)畫持續(xù)時(shí)間,0表示無動(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é)果分頁導(dǎo)航的容器-->
</body>
</html>
PHP實(shí)例queryInIndex.php:
PHP實(shí)例
<?php
include_once("../phpFiles/connMysql.php");//包含連接數(shù)據(jù)庫的代碼
$queryType=$_POST['queryType'];//目標(biāo)數(shù)據(jù)表
$queryKeywords=$_POST['queryKeywords']; //查詢?cè)~
$queryKeywords = preg_replace('#\s+#', ' ',$queryKeywords);//將字符串中多個(gè)連續(xù)空格替換為一個(gè)空格
$keywords=explode(" ",$queryKeywords); //查詢?cè)~按空格分割
//根據(jù)不同的數(shù)據(jù)表生成不同的查詢語句
if($queryType=="users")
{
$sqlsmt="select email,truename,enName,stuid,degree,supervisorname,tel,room from $queryType where "; //查詢語句的初始值
//利用循環(huán) 生成對(duì)每個(gè)關(guān)鍵詞的查詢語句
foreach ($keywords as $keyword)
{
$sqlsmt.="concat(email,truename,enName,stuid,degree,supervisorname,tel,room) like '%$keyword%' or ";//本句是關(guān)鍵,利用concat函數(shù)實(shí)現(xiàn)從多個(gè)字段查詢單個(gè)關(guān)鍵詞,并用' or '關(guān)鍵詞把每個(gè)關(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));
}
?>
PHP實(shí)例運(yùn)行結(jié)果截圖:
PHP實(shí)例
PHP實(shí)例
PHP實(shí)例更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP+ajax技巧與應(yīng)用小結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
PHP實(shí)例希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助.
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/966.html