在书写实现这个功能的过程中,我们会使用几个非常重要的asp函数,先来了解一下: Split() 函数将一个字符串分割并返回分割结果 表达式 Split (S[,d]) 实例: <% V= Split(A,B,C) For i = 0 To UBound(V) Response.Write V(i) Next %> 返回结果: A B C
UBound() 函数返回指定数组维数的最大可用下标. 表达式 Ubound(arrayname [, dimension]) 实例: <% i = Array("Monday","Tuesday","Wednesday") response.write UBound(i) %> 返回结果: 2
Rtrim() 函数去掉字符串右边的字符串. 表达式 Rtrim(string)
LTrim() 函数去掉字符串左边的空格. 表达式 LTrim(string)
REPLACE() 函数REPLACE()是最有用的字符串函数之一。你可以使用REPLACE()函数,以一个字符串取代另一个字符串的一部分。
下面咱们来看看实现的方法和代码:
searchdata=request("T1") '从文本框中获得输入的关键字 if trim(searchdata)="" then '如果没有关键字的输入或者只输入的空格,警告! response.write "<script>alert('请输入查询搜索关键字!');history.back();</script>" response.end end if
searchdata=replace(searchdata,"'","’")'将获得的字符串中英文单引号换成中文单引号 searchdata=Rtrim(LTrim(searchdata))'去掉获得的字符串最左边空格和最右边空格
sql1="" '网站关键字匹配sql语句 sql2="" '网站标题匹配sql语句 sql3="" '网站简要说明匹配sql语句
searchdatatmp=split(searchdata," ") '将输入的字符串根据空格分开,获得一个数组 max=ubound(searchdatatmp) '得出这个数组的维数,即输入的关键字个数
if max=0 then '如果max等于0说明只输入了一个关键字,那么就不需要循环处理 sql1=sql1&" key like '%" & searchdatatmp(i) & "%' " '网站关键字模糊搜索 sql2=sql2&" sitename like '%" & searchdatatmp(i) & "%' " '网站标题模糊搜索 sql3=sql3&" faq like '%" & searchdatatmp(i) & "%' " '网站简要说明搜索 else '如果含有多个关键字,采用循环处理sql语句 for i=0 to max '如果关键字很多,我们要求每一个搜索都要匹配每一个关键字,通过循环来实现 if i=0 then '写入下面sql语句作为开头,仔细研究一下下面的代码 sql1=sql1&" (key like '%" & searchdatatmp(i) & "%' and " sql2=sql2&" (sitename like '%" & searchdatatmp(i) & "%' and " sql3=sql3&" (faq like '%" & searchdatatmp(i) & "%' and " else if i=max then '如果循环到最后一个关键字,写入下面sql语句作为结尾 sql1=sql1&" key like '%" & searchdatatmp(i) & "%') " sql2=sql2&" sitename like '%" & searchdatatmp(i) & "%') " sql3=sql3&" faq like '%" & searchdatatmp(i) & "%') " else '如果关键自己不是开头的也不是结尾的,那么循坏写入下面的sql语句 sql1=sql1&" key like '%" & searchdatatmp(i) & "%' and " sql2=sql2&" sitename like '%" & searchdatatmp(i) & "%' and " sql3=sql3&" faq like '%" & searchdatatmp(i) & "%' and " end if end if next '循环结束 end if
sql="select * from www where "&sql1&" or "&sql2&" or "&sql3
response.write sql '看看我们这个代码运行的结果是否正确
在文本框中输入“网人 俱乐部”,然后点击搜索,记住表单的action="文件名",这个文件里包含以上的代码。
执行结果:select * from www where (key like '%网人%' and key like '%俱乐部%') or (sitename like '%网人%' and sitename like '%俱乐部%') or (faq like '%网人%' and faq like '%俱乐部%')
可以看出我们写的代码是正确的了。下一节我们讲解搜索出结果如果排序,以及怎么实现!
方案2:
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> |