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 '只有一个关键字时,就不用执行循环了.
sql=sql&" (I_title like '%"&keyword1(i)&"%' or I_Keyword like '%"&keyword1(i)&"%')"
else
for i=0 to Arrayi
if i=0 then'循环到第一个关键词时
sql=sql&" (I_title like '%"&keyword1(i)&"%' or I_Keyword like '%"&Keyword1(i)&"%') and"
else
if i=arrayi then '循环到最后关键词时
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
改进的类
<%
Class Search
Private objRequest
Private objRs
Private objConn
Private bolExactitude
'*********************************************************
' 初始化/终止程序
'*********************************************************
Private Sub Class_Initialize()
Dim DBPath
'确定使用何种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语句: 从外部读取数据库连接对象、查询条件
'*********************************************************
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 = ""
'依次读取输入的多关键字
For Each strItem in objRequest
strName = objRequest(strItem)
Next
strName = Rtrim(Ltrim(strName)) '去掉首尾空格
strNametmp = split(strName, " ") '将多关键字载入临时数组
strNamemax = Ubound(strNametmp) '获得临时数组的最大下标
'SQL多关键字查询核心
'单关键字
If bolExactitude = "" Then
If strNamemax = 0 Then
sqlF1 = sqlF1 & " Name LIKE '%" & strName & "%'"
sqlF2 = sqlF2 & " Tel LIKE '%" & strName & "%'"
sqlF3 = sqlF3 & " School LIKE '%" & strName & "%'"
Else
'多关键字
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 "目前通讯录中没有记录"
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 &"电话:"& str1 &"学校:"& str2 &"<br>"
objRs.MoveNext
Loop
End If
End Function
'*********************************************************
' 公有方法: 由外部调用输出结果
'*********************************************************
Public Function SearchOut()
SearchSql
End Function
End Class
%>
调用类处理
<!-- #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
'向类中传递数据库连接对象、查询条件
Set objFormSearch.Connection = objConn
objFormSearch.Exactitude = Request("Exactitude")
'调用内部方法输出查询结果
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>