ASP多个关键词搜索代码,关键词以+号或空格隔开
分类:程序开发 Tags: asp 关键词 评论:0 浏览:4989 最近更新时间:2008/10/18 23:47:05
方法一
方法二:(用类实现,不是很灵活)
表单
方法三
查询时可以输入多字符串,中间用空格隔开实现模糊查询,查询结果会将关键字用红色突出显示.
用法:
- 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>
查询时可以输入多字符串,中间用空格隔开实现模糊查询,查询结果会将关键字用红色突出显示.
- function seachKey(otypestr,keystr) '构造模糊查询语句,otypestr:查询字段,keystr:查询关键字
- dim tmpstr,MyArray,I
- MyArray = Split(keystr) '默认以空格分组
- For I = Lbound(MyArray) to Ubound(MyArray)
- if I=0 then
- tmpstr=otypestr & " like '%"&MyArray(I)&"%'"
- else
- tmpstr=tmpstr & " and " & otypestr & " like '%"&MyArray(I)&"%'"
- end if
- Next
- seachKey=tmpstr
- end function
- function seachResult(contentStr,keyStr)'用红粗突出显示查询结果
- Dim MyArray
- MyArray = Split(keyStr)
- For I = Lbound(MyArray) to Ubound(MyArray)
- contentStr=replace(contentStr,MyArray(I),"<font color=red><strong>"&MyArray(I)&"</strong></font>")
- next
- seachResult=contentStr
- end function
- dim strWhere=seachKey(otypestr,keystr)
- sql="select * from NEWS where "&strWhere&" order by id desc"
- 输入:当我们输入的keystr为“我们 函数 数组”时构造的sql语句如下面这样
- select * from NEWS where content like '%我们%' and content like '%函数%' and content like '%数组%' order by id desc
本文地址:/385/
-
• ASP多个关键词搜索代码,关键词以+号或空格隔开
• ASP将汉字转为拼音代码
• centos系统挂载数据盘方法
• ASP提交Post数据到远程网站的方法
• Asp超精准判断客户端浏览器类型代码
• ASP Access数据库连接关闭与.ldb文件锁定解决方法
• JS显示当前年月日时间分秒代码(时间不断变动)
• 中国十大ASP CMS系统介绍
• ASP过滤所有超链接代码
• MySQL报Field‘***’doesn’t have a default value错误的解决办法