运行环境:IIS
脚本语言:VBScript
数据库:Access/SQL Server
数据库语言:SQL
**1.概要:
不论是在论坛,还是新闻系统,或是下载系统等动态网站中,大家经常会看到搜索功能:搜索帖子,搜索用户,搜索软件(总之搜索关键字)等,本文则是介绍如何建立一个高效实用的,基于ASP的站内多值搜索。
本文面对的是“多条件模糊匹配搜索”,理解了多条件的,单一条件搜索也不过小菜一碟了。一般来讲,有两种方法进行多条件搜索:枚举法和递进法。搜索条件不太多时( n <=3),可使用枚举法,其语句频度为2的 n 次方,成指数增长, n 为条件数。很明显,当条件增多以后,无论从程序的效率还是可实现性考虑都应采用递进法,其语句频度为 n ,成线性增长。需要指出的是,枚举法思路非常简单,一一判断条件是否为空,再按非空条件搜索,同时可以利用真值表技术来对付条件极多的情况(相信没人去干这种事,4条件时就已经要写16组语句了);递进法的思想方法较为巧妙,重在理解,其巧就巧在一是使用了标志位(flag),二是妙用SQL中字符串连接符&。下面以实例来讲解引擎的建立。
**
**2.实例:
我们建立一通讯录查询引擎,数据库名为addressbook.mdb,表名为address,字段如下:
**
ID
|
Name
|
Tel
|
School
---|---|---|---
1
|
张 三
|
33333333
|
电子科技大学计算机系
2
|
李 四
|
44444444
|
四川大学生物系
3
|
王 二
|
22222222
|
西南交通大学建筑系
…
|
…
|
…
|
…
**Web搜索界面如下:
**
姓名:
|
电话:
|
学校:
|
搜索按钮
---|---|---|---
采用枚举法的源程序如下:
** ``` @ CODEPAGE = "936"
1
2**'连接数据库**
3
4** ```
5 **
6
7**dim conn**
8
9**dim DBOath**
10
11**dim rs**
12
13**dim sql**
14
15**Set conn=Server.CreateObject("ADODB.Connection")**
16
17**DBPath = Server.MapPath("addressbook.mdb")**
18
19**conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & DBPath **
20
21**Set rs=Server.CreateObject("ADODB.Recordset")**
22
23**'从Web页获取姓名、电话、学校的值**
24
25**dim Name**
26
27**dim Tel**
28
29**dim School**
30
31**Name=request("Name")**
32
33**Tel=request("Tel")**
34
35**School=request("School")**
36
37**'枚举法的搜索核心,因为有3个条件所以要写8组If判断语句**
38
39**if trim(Name)="" and trim(Tel)="" and trim(School)="" then**
40
41**sql="select * from address order by ID asc"**
42
43**end if**
44
45**if trim(Name)="" and trim(Tel)="" and trim(School) <>"" then **
46
47**sql="select * from address where School like '%" &trim(School)&"%' order by ID asc" **
48
49**end if**
50
51**if trim(Name)="" and trim(Tel) <>"" and trim(School)="" then **
52
53**sql="select * from address where Tel like '%" &trim(Tel)&"%' order by ID asc" **
54
55**end if**
56
57**if trim(Name)="" and trim(Tel) <>"" and trim(School)<>"" then **
58
59**sql="select * from address where Tel like '%" &trim(Tel)&"%' and School like '%"&trim(School)&"%' order by ID asc" **
60
61**end if**
62
63**if trim(Name) <>"" and trim(Tel)="" and trim(School)="" then **
64
65**sql="select * from address where Name like '%" &trim(Name)&"%' order by ID asc" **
66
67**end if**
68
69**if trim(Name) <>"" and trim(Tel)="" and trim(School)<>"" then **
70
71**sql="select * from address where Name like '%" &trim(Name)&"%' and School like '%"&trim(School)&"%' order by ID asc" **
72
73**end if**
74
75**if trim(Name) <>"" and trim(Tel)<>"" and trim(School)="" then **
76
77**sql="select * from address where Name like '%" &trim(Name)&"%' and Tel like '%"&trim(Tel)&"%' order by ID asc" **
78
79**end if**
80
81**if trim(Name) <>"" and trim(Tel)<>"" and trim(School)<>"" then **
82
83**sql="select * from address where Name like '%" &trim(Name)&"%' and Tel like '%"&trim(Tel)&"%' and School like '%"&trim(School)&"%' order by ID asc" **
84
85**end if**
86
87**rs.open sql,conn,1,1**
88
89**'显示搜索结果**
90
91**if rs.eof and rs.bof then**
92
93**response.write "目前通讯录中没有记录"**
94
95**else**
96
97**do while not rs.eof**
98
99**response.write "姓名:" &rs("Name")&"电话:"&rs("Tel")&"学校:"&rs("School")&"
<br/>
1" **
2
3**rs.movenext**
4
5**loop**
6
7**end if**
8
9**'断开数据库**
10
11**set rs=nothing**
12
13**conn.close**
14
15**set conn=nothing**
16
17**% >
18
19**
20
21**理解上述程序时,着重琢磨核心部分,8组语句一一对应了3个搜索框中的8种状态
22
23**
24
25**Name**
26
27|
28
29**Tel**
30
31|
32
33**School**
34
35---|---|---
36
37**空**
38
39|
40
41**空**
42
43|
44
45**空**
46
47**空**
48
49|
50
51**空**
52
53|
54
55**非空**
56
57**空**
58
59|
60
61**非空**
62
63|
64
65**空**
66
67**空**
68
69|
70
71**非空**
72
73|
74
75**非空**
76
77**非空**
78
79|
80
81**空**
82
83|
84
85**空**
86
87**非空**
88
89|
90
91**空**
92
93|
94
95**非空**
96
97**非空**
98
99|
100
101**非空**
102
103|
104
105**空**
106
107**非空**
108
109|
110
111**非空**
112
113|
114
115**非空**
116
117
118**另外trim()是VB的函数,将输入的字符串前后的空格去掉;%是SQL语言中的多字符通配符(_是单字符通配符),由此可见%" &trim()&"%对搜索框中输入的关键字是分别向左向右匹配的;SQL语言中用and连接说明非空条件之间是“与”关系。 **
119
120****
121
122**再来看看递进法,与枚举法相比它们只有核心部分不同:**
123
124**'递进法的搜索核心,依次判断条件为空否,非空则将其加入搜索条件**
125
126**sql="select * from address where"**
127
128**if Name <>"" then **
129
130**sql=sql &" Name like '%"&Name&"%' " **
131
132**flag=1**
133
134**end if**
135
136**if Tel <>"" and flag=1 then **
137
138**sql=sql &" and Tel like '%"&Tel&"%'" **
139
140**flag=1**
141
142**elseif Tel <>"" then **
143
144**sql=sql &" Tel like '%"&Tel&"%'" **
145
146**flag=1**
147
148**end if**
149
150**if Company <>"" and flag=1 then **
151
152**sql=sql &" and Company like '%"&Company&"%'" **
153
154**flag=1**
155
156**elseif Company <>"" then **
157
158**sql=sql &" Company like '%"&Company&"%'" **
159
160**flag=1**
161
162**end if**
163
164**if flag=0 then**
165
166**sql="select * from address order by ID asc"**
167
168**end if**
169
170**rs.open sql,conn,1,1
171
172**
173
174**递进法是一个明智的算法,单从语句的长短就可以看出来了。这个算法的难点和精髓就在flag和 &上。首先你应该清楚&在SQL中就是一个字符串连接符,把该符号左右的字符拼接在一起。再回到程序,当Name不为空时sql="select * from address where Name like '%"&Name&"%' "同时flag=1;接下来当Name不为空时且Tel不为空时,即Tel<>"" and flag=1时,sql="select * from address where Name like '%"&Name&"%' and Tel like '%"&Tel&"%' "同时flag=1,否则当Name为空Tel不为空,sql="select * from address where Tel like '%"&Tel&"%' "同时flag=1;以此类推就可以推广到n个条件的搜索。当然条件皆为空时,即flag=0将选择所有表中所有项。
175
176**
177
178**3.验证:
179
180至此,一个搜索引擎就建立起来了。以下是一些使用示例:
181
182**
183
184**姓名:张**
185
186|
187
188**电话:**
189
190|
191
192**学校:**
193
194|
195
196**搜索按钮**
197
198---|---|---|---
199
200****
201
202**搜索结果为:
203姓名: 张三 电话:33333333 单位:电子科技大学计算机系 **
204
205****
206
207**姓名:**
208
209|
210
211**电话:**
212
213|
214
215**学校:大学**
216
217|
218
219**搜索按钮**
220
221---|---|---|---
222
223****
224
225**搜索结果为:**
226
227**姓名:张三 电话:33333333 单位:电子科技大学计算机系**
228
229**姓名 李 四 电话:44444444 单位:四川大学生物系**
230
231**姓名:王二 电话:22222222 单位:西南交通大学建筑系
232
233**
234
235**姓名:**
236
237|
238
239**电话:4444**
240
241|
242
243**学校:四川**
244
245|
246
247**搜索按钮**
248
249---|---|---|---
250
251****
252
253**搜索结果为:**
254
255**姓名 李 四 电话:44444444 单位:四川大学生物系
256
257**
258
259**姓名:**
260
261|
262
263**电话:**
264
265|
266
267**学校:交%大**
268
269|
270
271**搜索按钮**
272
273---|---|---|---
274
275****
276
277**搜索结果为:**
278
279**姓名:王二 电话:22222222 单位:西南交通大学建筑系
280
281**
282
283**4.改进:
284其实这个引擎还有些缺陷,问题主要在于通配符%。一方面是因为人们平时习惯把*作为通配符,另一方面%若出现在超链接中,通过request获取时%将被“吃”掉,如下:
285
286\--test.htm--
287…
<a href="test.asp?content=test%the%sign">click here</a>
1…
2
3\--test.asp--
4<%
5content=request(“content”)
6response.write content
在IE中浏览test.htm时点击超链接,显示为:
testthesign
可见%直接被超链接忽略掉了。怎么才能解决这个问题呢?很简单,我们做点小小的手脚--偷梁换柱。
将以下代码加在搜索核心之前:
Name=replace(Name,"","%")
Tel=replace(Tel,"","%")
Company=replace(Company,"","%")
将以下代码加在搜索核心之后:
Name=replace(Name,"%","")
Tel=replace(Tel,"%","")
Company=replace(Company,"%","")
在我们来分析一下这些语句。replace()是VB中字符串替换函数,replace(Name,"","%") 就是将Name中所有的换成%。也就是说,我们把3个条件中凡是出现的都替换为%,这样一来前3句就将通配符改成了。而后3句就可以防止%被“吃”掉。所有问题就迎刃而解了吧。
**
姓名:
|
电话:
|
学校:交%大
|
搜索按钮
---|---|---|---
**搜索结果为:
姓名:王 二 电话:22222222 单位:西南交通大学建筑系
将上面的语句再改一改,把*用空格代替,不就成了我们在Google、BaiDu中常用的用空格来分开搜索条件的搜索引擎了吗? **
运行环境:IIS
脚本语言:VBScript
数据库:Access/SQL Server
数据库语言:SQL
**1.概要:
不论是在论坛,还是新闻系统,或是下载系统等动态网站中,大家经常会看到搜索功能:搜索帖子,搜索用户,搜索软件(总之搜索关键字)等,本文则是介绍如何建立一个高效实用的,基于ASP的站内多值搜索。
本文面对的是“多条件模糊匹配搜索”,理解了多条件的,单一条件搜索也不过小菜一碟了。一般来讲,有两种方法进行多条件搜索:枚举法和递进法。搜索条件不太多时( n <=3),可使用枚举法,其语句频度为2的 n 次方,成指数增长, n 为条件数。很明显,当条件增多以后,无论从程序的效率还是可实现性考虑都应采用递进法,其语句频度为 n ,成线性增长。需要指出的是,枚举法思路非常简单,一一判断条件是否为空,再按非空条件搜索,同时可以利用真值表技术来对付条件极多的情况(相信没人去干这种事,4条件时就已经要写16组语句了);递进法的思想方法较为巧妙,重在理解,其巧就巧在一是使用了标志位(flag),二是妙用SQL中字符串连接符&。下面以实例来讲解引擎的建立。
**
**2.实例:
我们建立一通讯录查询引擎,数据库名为addressbook.mdb,表名为address,字段如下:
**
ID
|
Name
|
Tel
|
School
---|---|---|---
1
|
张 三
|
33333333
|
电子科技大学计算机系
2
|
李 四
|
44444444
|
四川大学生物系
3
|
王 二
|
22222222
|
西南交通大学建筑系
…
|
…
|
…
|
…
**Web搜索界面如下:
**
姓名:
|
电话:
|
学校:
|
搜索按钮
---|---|---|---
采用枚举法的源程序如下:
** ``` @ CODEPAGE = "936"
1
2**'连接数据库**
3
4** ```
5 **
6
7**dim conn**
8
9**dim DBOath**
10
11**dim rs**
12
13**dim sql**
14
15**Set conn=Server.CreateObject("ADODB.Connection")**
16
17**DBPath = Server.MapPath("addressbook.mdb")**
18
19**conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & DBPath **
20
21**Set rs=Server.CreateObject("ADODB.Recordset")**
22
23**'从Web页获取姓名、电话、学校的值**
24
25**dim Name**
26
27**dim Tel**
28
29**dim School**
30
31**Name=request("Name")**
32
33**Tel=request("Tel")**
34
35**School=request("School")**
36
37**'枚举法的搜索核心,因为有3个条件所以要写8组If判断语句**
38
39**if trim(Name)="" and trim(Tel)="" and trim(School)="" then**
40
41**sql="select * from address order by ID asc"**
42
43**end if**
44
45**if trim(Name)="" and trim(Tel)="" and trim(School) <>"" then **
46
47**sql="select * from address where School like '%" &trim(School)&"%' order by ID asc" **
48
49**end if**
50
51**if trim(Name)="" and trim(Tel) <>"" and trim(School)="" then **
52
53**sql="select * from address where Tel like '%" &trim(Tel)&"%' order by ID asc" **
54
55**end if**
56
57**if trim(Name)="" and trim(Tel) <>"" and trim(School)<>"" then **
58
59**sql="select * from address where Tel like '%" &trim(Tel)&"%' and School like '%"&trim(School)&"%' order by ID asc" **
60
61**end if**
62
63**if trim(Name) <>"" and trim(Tel)="" and trim(School)="" then **
64
65**sql="select * from address where Name like '%" &trim(Name)&"%' order by ID asc" **
66
67**end if**
68
69**if trim(Name) <>"" and trim(Tel)="" and trim(School)<>"" then **
70
71**sql="select * from address where Name like '%" &trim(Name)&"%' and School like '%"&trim(School)&"%' order by ID asc" **
72
73**end if**
74
75**if trim(Name) <>"" and trim(Tel)<>"" and trim(School)="" then **
76
77**sql="select * from address where Name like '%" &trim(Name)&"%' and Tel like '%"&trim(Tel)&"%' order by ID asc" **
78
79**end if**
80
81**if trim(Name) <>"" and trim(Tel)<>"" and trim(School)<>"" then **
82
83**sql="select * from address where Name like '%" &trim(Name)&"%' and Tel like '%"&trim(Tel)&"%' and School like '%"&trim(School)&"%' order by ID asc" **
84
85**end if**
86
87**rs.open sql,conn,1,1**
88
89**'显示搜索结果**
90
91**if rs.eof and rs.bof then**
92
93**response.write "目前通讯录中没有记录"**
94
95**else**
96
97**do while not rs.eof**
98
99**response.write "姓名:" &rs("Name")&"电话:"&rs("Tel")&"学校:"&rs("School")&"
<br/>
1" **
2
3**rs.movenext**
4
5**loop**
6
7**end if**
8
9**'断开数据库**
10
11**set rs=nothing**
12
13**conn.close**
14
15**set conn=nothing**
16
17**% >
18
19**
20
21**理解上述程序时,着重琢磨核心部分,8组语句一一对应了3个搜索框中的8种状态
22
23**
24
25**Name**
26
27|
28
29**Tel**
30
31|
32
33**School**
34
35---|---|---
36
37**空**
38
39|
40
41**空**
42
43|
44
45**空**
46
47**空**
48
49|
50
51**空**
52
53|
54
55**非空**
56
57**空**
58
59|
60
61**非空**
62
63|
64
65**空**
66
67**空**
68
69|
70
71**非空**
72
73|
74
75**非空**
76
77**非空**
78
79|
80
81**空**
82
83|
84
85**空**
86
87**非空**
88
89|
90
91**空**
92
93|
94
95**非空**
96
97**非空**
98
99|
100
101**非空**
102
103|
104
105**空**
106
107**非空**
108
109|
110
111**非空**
112
113|
114
115**非空**
116
117
118**另外trim()是VB的函数,将输入的字符串前后的空格去掉;%是SQL语言中的多字符通配符(_是单字符通配符),由此可见%" &trim()&"%对搜索框中输入的关键字是分别向左向右匹配的;SQL语言中用and连接说明非空条件之间是“与”关系。 **
119
120****
121
122**再来看看递进法,与枚举法相比它们只有核心部分不同:**
123
124**'递进法的搜索核心,依次判断条件为空否,非空则将其加入搜索条件**
125
126**sql="select * from address where"**
127
128**if Name <>"" then **
129
130**sql=sql &" Name like '%"&Name&"%' " **
131
132**flag=1**
133
134**end if**
135
136**if Tel <>"" and flag=1 then **
137
138**sql=sql &" and Tel like '%"&Tel&"%'" **
139
140**flag=1**
141
142**elseif Tel <>"" then **
143
144**sql=sql &" Tel like '%"&Tel&"%'" **
145
146**flag=1**
147
148**end if**
149
150**if Company <>"" and flag=1 then **
151
152**sql=sql &" and Company like '%"&Company&"%'" **
153
154**flag=1**
155
156**elseif Company <>"" then **
157
158**sql=sql &" Company like '%"&Company&"%'" **
159
160**flag=1**
161
162**end if**
163
164**if flag=0 then**
165
166**sql="select * from address order by ID asc"**
167
168**end if**
169
170**rs.open sql,conn,1,1
171
172**
173
174**递进法是一个明智的算法,单从语句的长短就可以看出来了。这个算法的难点和精髓就在flag和 &上。首先你应该清楚&在SQL中就是一个字符串连接符,把该符号左右的字符拼接在一起。再回到程序,当Name不为空时sql="select * from address where Name like '%"&Name&"%' "同时flag=1;接下来当Name不为空时且Tel不为空时,即Tel<>"" and flag=1时,sql="select * from address where Name like '%"&Name&"%' and Tel like '%"&Tel&"%' "同时flag=1,否则当Name为空Tel不为空,sql="select * from address where Tel like '%"&Tel&"%' "同时flag=1;以此类推就可以推广到n个条件的搜索。当然条件皆为空时,即flag=0将选择所有表中所有项。
175
176**
177
178**3.验证:
179
180至此,一个搜索引擎就建立起来了。以下是一些使用示例:
181
182**
183
184**姓名:张**
185
186|
187
188**电话:**
189
190|
191
192**学校:**
193
194|
195
196**搜索按钮**
197
198---|---|---|---
199
200****
201
202**搜索结果为:
203姓名: 张三 电话:33333333 单位:电子科技大学计算机系 **
204
205****
206
207**姓名:**
208
209|
210
211**电话:**
212
213|
214
215**学校:大学**
216
217|
218
219**搜索按钮**
220
221---|---|---|---
222
223****
224
225**搜索结果为:**
226
227**姓名:张三 电话:33333333 单位:电子科技大学计算机系**
228
229**姓名 李 四 电话:44444444 单位:四川大学生物系**
230
231**姓名:王二 电话:22222222 单位:西南交通大学建筑系
232
233**
234
235**姓名:**
236
237|
238
239**电话:4444**
240
241|
242
243**学校:四川**
244
245|
246
247**搜索按钮**
248
249---|---|---|---
250
251****
252
253**搜索结果为:**
254
255**姓名 李 四 电话:44444444 单位:四川大学生物系
256
257**
258
259**姓名:**
260
261|
262
263**电话:**
264
265|
266
267**学校:交%大**
268
269|
270
271**搜索按钮**
272
273---|---|---|---
274
275****
276
277**搜索结果为:**
278
279**姓名:王二 电话:22222222 单位:西南交通大学建筑系
280
281**
282
283**4.改进:
284其实这个引擎还有些缺陷,问题主要在于通配符%。一方面是因为人们平时习惯把*作为通配符,另一方面%若出现在超链接中,通过request获取时%将被“吃”掉,如下:
285
286\--test.htm--
287…
<a href="test.asp?content=test%the%sign">click here</a>
1…
2
3\--test.asp--
4<%
5content=request(“content”)
6response.write content
在IE中浏览test.htm时点击超链接,显示为:
testthesign
可见%直接被超链接忽略掉了。怎么才能解决这个问题呢?很简单,我们做点小小的手脚--偷梁换柱。
将以下代码加在搜索核心之前:
Name=replace(Name,"","%")
Tel=replace(Tel,"","%")
Company=replace(Company,"","%")
将以下代码加在搜索核心之后:
Name=replace(Name,"%","")
Tel=replace(Tel,"%","")
Company=replace(Company,"%","")
在我们来分析一下这些语句。replace()是VB中字符串替换函数,replace(Name,"","%") 就是将Name中所有的换成%。也就是说,我们把3个条件中凡是出现的都替换为%,这样一来前3句就将通配符改成了。而后3句就可以防止%被“吃”掉。所有问题就迎刃而解了吧。
**
姓名:
|
电话:
|
学校:交%大
|
搜索按钮
---|---|---|---
**搜索结果为:
姓名:王 二 电话:22222222 单位:西南交通大学建筑系
将上面的语句再改一改,把*用空格代替,不就成了我们在Google、BaiDu中常用的用空格来分开搜索条件的搜索引擎了吗? **