keyword=trim(request("keyword"))
strkeyword=instr(keyword," ")
if strkeyword=0 then'是否爲+号
keyword1=split(keyword,"+")
else
keyword1=split(keyword," ")
end if
Arrayi=ubound(keyword1)
if arrayi=0 then '隻有一個(gè)關鍵字時(shí),就不用(yòng)執行循環了(le).
sql=sql&" (I_title like '%"&keyword1(i)&"%' or I_Keyword like '%"&keyword1(i)&"%')"
else
for i=0 to Arrayi
if i=0 then'循環到第一個(gè)關鍵詞時(shí)
sql=sql&" (I_title like '%"&keyword1(i)&"%' or I_Keyword like '%"&Keyword1(i)&"%') and"
else
if i=arrayi then '循環到最後關鍵詞時(shí)
sql=sql&" (I_title like '%"&keyword1(i)&"%' or I_Keyword like '%"&Keyword1(i)&"%')"
else
sql=sql&" (I_title like '%"&keyword1(i)&"%' or I_Keyword like '%"&Keyword1(i)&"%') and"
end if
end if
next
end if
改進的(de)類
<%
Class Search
Private objRequest
Private objRs
Private objConn
Private bolExactitude
'*********************************************************
' 初始化(huà)/終止程序
'*********************************************************
Private Sub Class_Initialize()
Dim DBPath
'确定使用(yòng)何種Request集合
If Ucase(Request("Collection")) = "QUERYSTRING" Then
Set objRequest = Request.QueryString
Else
Set objRequest = Request.Form
End If
Set objRs = Server.CreateObject("ADODB.Recordset")
End Sub
Private Sub Class_Terminate()
Set objRequest = Nothing
Set objRs = Nothing
Set objConn = Nothing
End Sub
'*********************************************************
' Set語句: 從外部讀取數據庫連接對(duì)象、查詢條件
'*********************************************************
Public Property Let Exactitude(strExactitude)
bolExactitude = strExactitude
End Property
Public Property Set Connection(objConnection)
Set objConn = objConnection
End Property
'*********************************************************
' 私有方法: 模糊查詢并“輸出結果”
'*********************************************************
Private Function SearchSql()
Dim strItem, strName, strNametmp, strNamemax, Item
Dim sqlF1, sqlF2, sqlF3, sqlSearch
sqlF1 = ""
sqlF2 = ""
sqlF3 = ""
'依次讀取輸入的(de)多(duō)關鍵字
For Each strItem in objRequest
strName = objRequest(strItem)
Next
strName = Rtrim(Ltrim(strName)) '去掉首尾空格
strNametmp = split(strName, " ") '将多(duō)關鍵字載入臨時(shí)數組
strNamemax = Ubound(strNametmp) '獲得(de)臨時(shí)數組的(de)最大(dà)下(xià)标
'SQL多(duō)關鍵字查詢核心
'單關鍵字
If bolExactitude = "" Then
If strNamemax = 0 Then
sqlF1 = sqlF1 & " Name LIKE '%" & strName & "%'"
sqlF2 = sqlF2 & " Tel LIKE '%" & strName & "%'"
sqlF3 = sqlF3 & " School LIKE '%" & strName & "%'"
Else
'多(duō)關鍵字
For Item = 0 to strNamemax
If Item = 0 then
sqlF1 = sqlF1 & " (Name LIKE '%" & strNametmp(Item) & "%' OR "
sqlF2 = sqlF2 & " (Tel LIKE '%" & strNametmp(Item) & "%' OR "
sqlF3 = sqlF3 & " (School LIKE '%" & strNametmp(Item) & "%' OR "
Else
If Item = strNamemax then
sqlF1 = sqlF1 & " Name LIKE '%" & strNametmp(Item) & "%') "
sqlF2 = sqlF2 & " Tel LIKE '%" & strNametmp(Item) & "%') "
sqlF3 = sqlF3 & " School LIKE '%" & strNametmp(Item) & "%') "
Else
sqlF1 = sqlF1 & " Name LIKE '%" & strNametmp(Item) & "%' OR "
sqlF2 = sqlF2 & " Tel LIKE '%" & strNametmp(Item) & "%' OR "
sqlF3 = sqlF3 & " School LIKE '%" & strNametmp(Item) & "%' OR "
End If
End If
Next
End If
Else
If strNamemax = 0 Then
sqlF1 = sqlF1 & " [Name] = '"&strName&"'"
sqlF2 = sqlF2 & " [Tel] = '"&strName&"'"
sqlF3 = sqlF3 & " [School] = '"&strName&"'"
End If
End If
sqlSearch = "SELECT * FROM [data] WHERE "&sqlF1&" OR "&sqlF2&" OR "&sqlF3
objRs.Open sqlSearch,objConn,1,1
'輸出查詢結果
Dim str, str1, str2
If objRs.EOF And objRs.BOF Then
Response.Write "目前通(tōng)訊錄中沒有記錄"
Else
Do While Not objRs.EOF
'将關鍵字(單)變成紅色
str = Replace(objRs("Name"), strName, "<b style='color:#FF6347'>" & strName & "</b>")
str1 = Replace(objRs("Tel"), strName, "<b style='color:#FF6347'>" & strName & "</b>")
str2 = Replace(objRs("School"),trim(strName),"<b style='color:#FF6347'>" & trim(strName) & "</b>")
Response.Write "姓名:"& str &"電話(huà):"& str1 &"學校:"& str2 &"<br>"
objRs.MoveNext
Loop
End If
End Function
'*********************************************************
' 公有方法: 由外部調用(yòng)輸出結果
'*********************************************************
Public Function SearchOut()
SearchSql
End Function
End Class
%>
調用(yòng)類處理(lǐ)
<!-- #include file="searchclass.asp" -->
<%
Dim objFormSearch
Set objFormSearch = New Search
Set objConn = Server.CreateObject("ADODB.Connection")
DBPath = Server.MapPath("search.mdb")
objConn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & DBPath
'向類中傳遞數據庫連接對(duì)象、查詢條件
Set objFormSearch.Connection = objConn
objFormSearch.Exactitude = Request("Exactitude")
'調用(yòng)内部方法輸出查詢結果
Response.Write objFormSearch.SearchOut()
Response.Write objFormSearch.Out()
%>
表單
<%@ CODEPAGE = "936" %>
<form method="post" action="sfc.asp">
<input type="hidden" name="Collection" value="Form">
<input type="radio" name="Exactitude" value="True">
name:<input type="text" name="name!^d{3}-/d{3}--/d{4}$">
<input type="submit" value="Go">
</form>