以后地位:首页 > 建站常识 > 列表

ASP多个关头词搜刮代码,关头词以+号或空格离隔

点击: 次时候:2016/4/13关头词:ASP 多个关头词 搜刮
<% if request.querystring("action") = "search" then '判定领受到的action值是不是为search,若是是的,则起头搜刮 dim strsql dim strconn dim objrs dim
      <%
      if request.querystring("action") = "search" then
      '判定领受到的action值是不是为search,若是是的,则起头搜刮
      dim strsql
      dim strconn
      dim objrs
      dim strkeyword
      '界说数据库链接串
      strconn = "provider=microsoft.jet.oledb.4.0;data
      source="&server.mappath("**.mdb")
      set objconn = server.createobject("adodb.connection")
      objconn.open strconn
      strkeyword = request.form("keyword") '取得表单中的关头字
      keywords=Split(strkeyword,"|") '起头提取并键字
      i=0
      strsql="select * from 这里填写字段名 where"
      for each key in keywords
      strsql=strsql+" or title like '%"&keywords(i)&"%'"
      i=i+1
      next
      strsql=WordStr(strsql, "where or", "where") '把SQL语句里的where or 替代成where
      set objrs = server.createobject("adodb.recordset")
      objrs.open strsql, objconn,1,3
      if not rs.eof then '判定是不是有记实,若是有则轮回显现
      while not objrs.eof
      response.write "" &objrs("字段名") &"
" '这里要改成想要显现的字段
      objrs.movenext
      wend
      else '若是指针到开端,则表现不记实
      response.write "不记实"
      end if
      end if
      %>
     



      请输出关头字,若是有多个请用"|"分开:
      查找标题含有
     

     
     ================================================================
方式一
1. keyword=trim(request("keyword"))
2. strkeyword=instr(keyword," ")
3. if strkeyword=0 then'是不是为+号
4. keyword1=split(keyword,"+")
5. else
6. keyword1=split(keyword," ")
7. end if
8. Arrayi=ubound(keyword1)
9. if arrayi=0 then '只要一个关头字时,就不必履行轮回了.
10. sql=sql&" (I_title like '%"&keyword1(i)&"%' or I_Keyword like '%"&keyword1(i)&"%')"
11. else
12. for i=0 to Arrayi
13. if i=0 then'轮回到第一个关头词时
14. sql=sql&" (I_title like '%"&keyword1(i)&"%' or I_Keyword like '%"&Keyword1(i)&"%') and"
15. else
16. if i=arrayi then '轮回到最初关头词时
17. sql=sql&" (I_title like '%"&keyword1(i)&"%' or I_Keyword like '%"&Keyword1(i)&"%')"
18. else
19. sql=sql&" (I_title like '%"&keyword1(i)&"%' or I_Keyword like '%"&Keyword1(i)&"%') and"
20. end if
21. end if
22. next
23. end if


方式二:(用类完成,不是很矫捷)
1. <%
2. Class Search
3. Private objRequest
4. Private objRs
5. Private objConn
6. Private bolExactitude
7.
8.
9. '*********************************************************
10. ' 初始化/停止法式
11. '*********************************************************
12. Private Sub Class_Initialize()
13. Dim DBPath
14. '肯定利用何种Request调集
15. If Ucase(Request("Collection")) = "QUERYSTRING" Then
16. Set objRequest = Request.QueryString
17. Else
18. Set objRequest = Request.Form
19. End If
20.
21. Set objRs = Server.CreateObject("ADODB.Recordset")
22. End Sub
23.
24. Private Sub Class_Terminate()
25. Set objRequest = Nothing
26. Set objRs = Nothing
27. Set objConn = Nothing
28. End Sub
29.
30.
31. '*********************************************************
32. ' Set语句: 从外部读取数据库毗连工具、查问前提
33. '*********************************************************
34. Public Property Let Exactitude(strExactitude)
35. bolExactitude = strExactitude
36. End Property
37.
38. Public Property Set Connection(objConnection)
39. Set objConn = objConnection
40. End Property
41.
42.
43. '*********************************************************
44. ' 私有方式: 恍惚查问并“输出成果”
45. '*********************************************************
46. Private Function SearchSql()
47. Dim strItem, strName, strNametmp, strNamemax, Item
48. Dim sqlF1, sqlF2, sqlF3, sqlSearch
49. sqlF1 = ""
50. sqlF2 = ""
51. sqlF3 = ""
52. '顺次读取输出的多关头字
53. For Each strItem in objRequest
54. strName = objRequest(strItem)
55. Next
56. strName = Rtrim(Ltrim(strName)) '去掉首尾空格
57. strNametmp = split(strName, " ") '将多关头字载入姑且数组
58. strNamemax = Ubound(strNametmp) '取得姑且数组的最大下标
59.
60. 'SQL多关头字查问焦点
61. '单关头字
62. If bolExactitude = "" Then
63. If strNamemax = 0 Then
64. sqlF1 = sqlF1 & " Name LIKE '%" & strName & "%'"
65. sqlF2 = sqlF2 & " Tel LIKE '%" & strName & "%'"
66. sqlF3 = sqlF3 & " School LIKE '%" & strName & "%'"
67. Else
68. '多关头字
69. For Item = 0 to strNamemax
70. If Item = 0 then
71. sqlF1 = sqlF1 & " (Name LIKE '%" & strNametmp(Item) & "%' OR "
72. sqlF2 = sqlF2 & " (Tel LIKE '%" & strNametmp(Item) & "%' OR "
73. sqlF3 = sqlF3 & " (School LIKE '%" & strNametmp(Item) & "%' OR "
74. Else
75. If Item = strNamemax then
76. sqlF1 = sqlF1 & " Name LIKE '%" & strNametmp(Item) & "%') "
77. sqlF2 = sqlF2 & " Tel LIKE '%" & strNametmp(Item) & "%') "
78. sqlF3 = sqlF3 & " School LIKE '%" & strNametmp(Item) & "%') "
79. Else
80. sqlF1 = sqlF1 & " Name LIKE '%" & strNametmp(Item) & "%' OR "
81. sqlF2 = sqlF2 & " Tel LIKE '%" & strNametmp(Item) & "%' OR "
82. sqlF3 = sqlF3 & " School LIKE '%" & strNametmp(Item) & "%' OR "
83. End If
84. End If
85. Next
86. End If
87. Else
88. If strNamemax = 0 Then
89. sqlF1 = sqlF1 & " [Name] = '"&strName&"'"
90. sqlF2 = sqlF2 & " [Tel] = '"&strName&"'"
91. sqlF3 = sqlF3 & " [School] = '"&strName&"'"
92. End If
93. End If
94. sqlSearch = "SELECT * FROM [data] WHERE "&sqlF1&" OR "&sqlF2&" OR "&sqlF3
95. objRs.Open sqlSearch,objConn,1,1
96.
97. '输出查问成果
98. Dim str, str1, str2
99. If objRs.EOF And objRs.BOF Then
100. Response.Write "今朝通信录中不记实"
101. Else
102. Do While Not objRs.EOF
103. '将关头字(单)变成白色
104. str = WordStr(objRs("Name"), strName, "" & strName & "")
105. str1 = WordStr(objRs("Tel"), strName, "" & strName & "")
106. str2 = WordStr(objRs("School"),trim(strName),"" & trim(strName) & "")
107. Response.Write "姓名:"& str &"德律风:"& str1 &"黉舍:"& str2 &"
"
108. objRs.MoveNext
109. Loop
110. End If
111. End Function
112.
113.
114. '*********************************************************
115. ' 私有方式: 由外部挪用输出成果
116. '*********************************************************
117. Public Function SearchOut()
118. SearchSql
119. End Function
120. End Class
121. %>
122.
123. 挪用类处置
124.
125. <%
126. Dim objFormSearch
127. Set objFormSearch = New Search
128.
129. Set objConn = Server.CreateObject("ADODB.Connection")
130. DBPath = Server.MapPath("search.mdb")
131. objConn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & DBPath
132.
133. '向类中通报数据库毗连工具、查问前提
134. Set objFormSearch.Connection = objConn
135. objFormSearch.Exactitude = Request("Exactitude")
136.
137. '挪用外部方式输出查问成果
138. Response.Write objFormSearch.SearchOut()
139.
140. Response.Write objFormSearch.Out()
141. %>
表单
1. <%@ CODEPAGE = "936" %>
2.


3.
4.
5. name:
6.
7.


方式三
查问时能够输出多字符串,中心用空格离隔完成恍惚查问,查问成果会将关头字用白色凸起显现.
1. function seachKey(otypestr,keystr) '机关恍惚查问语句,otypestr:查问字段,keystr:查问关头字
2. dim tmpstr,MyArray,I
3. MyArray = Split(keystr) '默许以空格分组
4. For I = Lbound(MyArray) to Ubound(MyArray)
5. if I=0 then
6. tmpstr=otypestr & " like '%"&MyArray(I)&"%'"
7. else
8. tmpstr=tmpstr & " and " & otypestr & " like '%"&MyArray(I)&"%'"
9. end if
10. Next
11. seachKey=tmpstr
12. end function
13.
14. function seachResult(contentStr,keyStr)'用红粗凸起显现查问成果
15. Dim MyArray
16. MyArray = Split(keyStr)
17. For I = Lbound(MyArray) to Ubound(MyArray)
18. contentStr=replace(contentStr,MyArray(I),""&MyArray(I)&"")
19. next
20. seachResult=contentStr
21. end function
用法:
1. dim strWhere=seachKey(otypestr,keystr)
2. sql="select * from NEWS where "&strWhere&" order by id desc"
3. 输出:当咱们输出的keystr为“咱们 函数 数组”时机关的sql语句以下面如许
4. select * from NEWS where content like '%咱们%' and content like '%函数%' and content like '%数组%' order by id desc
============================================================
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),""&MyArray(I)&"")
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

预定建站
收费供给网站优化
支付关头词