有关用dw mx制作站内搜索的问题贴子在论坛不断地有人提问,因此也就出了这一篇教程,希望对你有所帮助。
站内搜索可分为单参数(根据一个条件查询一个字段)查询和多参数(多个条件可选查询不同的数据库字段)查询,单参数查询在dw mx的简单记录集中把筛选条件选择为“包含”就行了,这时切换到“高级记录集”模式,sql语句如下:
select *
from user
where uesr like '%mmcolparam%'
user为要查询的数据库表,uesr为要查询的字段,mmcolparam为查询变量,从表单提交数据时,把文本域的值赋值给该变量,%%为通配符,like为逻辑运算符,意为“类似于”,用于搜索包含关键词的记录。以上这些代码是dw mx自动生成的,比较简单。下面我们要实现的是多参数查询,即多个条件,查询多个数据库字段,把符合搜索条件的记录筛出。
这是本人用dw mx做的多参数站内搜索,可根据多个条件查询记录。
实例一、根据两个条件搜索
操作步骤:
1、设计好你的数据库表,本例用是论坛贴子表,如下图所示:
bbs_title字段为贴子标题,bbs_uesr字段为主题作者。现在要求,用户可在查询页面选择以标题或作者搜索。
2、新建一个页面,保存为htm或asp(如果有asp代码),本例保存为search.asp。插入表单、一个文本域和一个列表菜单,表单方法为get,对应名称和值如下:
| 文本域名称 | 说明 | text | 关键词 | select | 列表菜单,先择搜索条件 |
列表菜单设置以下:
页面样式:
为了实例示方便,搜索界面和搜索结果页在同一个页面中,即search.asp。
3、插入记录集
启动“记录集”对话框,在简单记录集中设置好连接和数据数据库表,如下:
添加一个where子句到sql文本框中,如下图:
上图中的and bbs_dist=0是为了搜索出为贴子主题的记录,你可以删除它,如:
where mm_sch like '%mm_text%'
变量设置如下:
这样,一个多参数搜索页面就做成了,把记录集相关字段绑定到页面,
输入“李飞”,选择“作者”,搜索结果如下:
输入“李飞”,选择“标题”,搜索结果如下:
技巧:为了把标题中关键词以不同颜色显示(如上图所示),标题字段值用一个函数替换关键词,如下:
<%=replace((recordset1.fields.item("bbs_title").value),request.querystring("text"), "<font color='#ff0000'>"&request.querystring("text")&"</font>")%>
把(recordset1.fields.item("bbs_title").value)换成你的字段,并且将text修改为你设计的文本域名称。
实例二,根据标题、作者和发表时间搜索记录
1、在例一的基础增加一个列表菜单,名称为tima,设置如下:
上图中的“不选定条件”值为空,稍后在记录集中设置它的默认值。其它的你自己可以任意添加选项和时间,一天前值为1,一年前值为365,依此类推。
2、打开高级记录集,设置如下:
上图那个mm_tima变量的默认值99999999是刚才“不选条件”值为空时,返回99999999,即查询99999999天前的记录,这个数值你可以自已设置,如果不选定时间,就查询你的数据中第一条记录插入时的时间,这样就搜索数据库的全部记录了。
这里比上例多了一个时间筛选条件bbs_tima>=now()-mm_tima,nwo()是获取当前系统日期时间的一个函数,用它减去选定多少天前的变量值,即可筛出发表时间大于或等于多少天前的记录。
以上两例都是单层sql查询,你可以依照以上介绍的方法添and条件,如果要同时查询两个字段,除了用and外,还可用or运算符,比如 where bbs_title like '%mm_title%' or bbs_uesr like '%mm_uesr%' ,它查询bbs_title或者bbs_uesr字段符合搜索条件的记录。但有些记录,比如论坛回复贴子,因为它没有标题,bbs_title字段是空的,而我要根据回复内容搜索贴子时,就不能显示出主题标题。如何解决这个问题呢?用嵌套查询可实现这个目的。
实例三、按标题、作者、回复内容及发表时间搜索记录。
1\把例二中的select列表菜单添多一项“回复内容”,如下图所示:
2、修改代码
切换到“代码”视图,找到这句代码:
recordset1.source = "select * from bbs where " + replace(recordset1__mm_sch, "'", "''") + " like '%" + replace(recordset1__mm_text, "'", "''") + "%'and bbs_tima>=now()-" + replace(recordset1__mm_tima, "'", "''") + " and bbs_dist=0 order by bbsid desc"
这是记录集sql查询语句,把它改为:
recordset1.source = sql
然在在记录集代码段的第一行输入:
dim sql
if recordset1__mm_sch="bbs_book" then
sql=" select * from bbs where bbsid in (select bbs_dist from bbs where " + replace(recordset1__mm_sch, "'", "''") + " like '%" + replace(recordset1__mm_text, "'", "''") + "%'and bbs_tima>=now()-" + replace(recordset1__mm_tima, "'", "''") + " and bbs_dist<>0 order by bbsid desc) order by bbsid desc"
else
sql="select * from bbs where " + replace(recordset1__mm_sch, "'", "''") + " like '%" + replace(recordset1__mm_text, "'", "''") + "%'and bbs_tima>=now()-" + replace(recordset1__mm_tima, "'", "''") + " and bbs_dist=0 order by bbsid desc"
end if
放入位置下图(选中部分):
以上代码的简要说明:
由于要用到嵌套查询,所以使用判断语句根据用户选定的条件切换sql语句,比如,当用户选择“回复内容”时,菜单列表返回值为“bbs_book”,执行下面这一段sql语句:
sql=" select * from bbs where bbsid in (select bbs_dist from bbs where " + replace(recordset1__mm_sch, "'", "''") + " like '%" + replace(recordset1__mm_text, "'", "''") + "%'and bbs_tima>=now()-" + replace(recordset1__mm_tima, "'", "''") + " and bbs_dist<>0 order by bbsid desc) order by bbsid desc"
这是嵌套查询的语句,小括号()内的是子级查询,把符合搜索条件的记录筛出,然后把bbs_dist(注:这个字段记录主题id号,对应所回复的主题的id号)字段的值以列表形式返回主查询继续下一次查询,比如,程序执行时,()内的sql语句优先执行,筛出符合条件的记录后,把它的bbs_dist的值返回主查询查询,如下:
select * from bbs where bbsid in (12,35,41,45,87...)
这样,主查询语句就能把bbsid(id号)等于12、35、41、45、87的记录全部筛出,而这些记录都是有标题的贴子。bbs_dist<>0表示只查询所有回复内容,不查询主题内容。
须要注意的是,子查询select子句只能返回一个字段的值,如select bbs_dist,否则将报错,若写成这样:
select bbs_dist,bbs_id
报错如下:
如果不是选中“回复内容”,将由else切换为:
sql="select * from bbs where " + replace(recordset1__mm_sch, "'", "''") + " like '%" + replace(recordset1__mm_text, "'", "''") + "%'and bbs_tima>=now()-" + replace(recordset1__mm_tima, "'", "''") + " and bbs_dist=0 order by bbsid desc"
以上这段代码是单层查询,和第二例sql语句是一样的。
好了,关于用dw mx做站内搜索就讲到这里,只要核心原理都告诉给你了,留一些思考的空间给你吧,自己去试验扩展它的功能。俺没时间了,就此告别。886!
下面是搜索结果页源代码(搜索页和结果页合拼在一起,实际上应把它分离开来):
<%@language="vbscript" codepage="936"%>
<!--#include file="connections/new.asp" -->
<%
dim recordset1__mm_text
recordset1__mm_text = "1"
if (request.querystring("text") <> "") then
recordset1__mm_text = request.querystring("text")
end if
%>
<%
dim recordset1__mm_sch
recordset1__mm_sch = "1"
if (request.querystring("select") <> "") then
recordset1__mm_sch = request.querystring("select")
end if
%>
<%
dim recordset1__mm_tima
recordset1__mm_tima = "99999999"
if (request.querystring("tima") <> "") then
recordset1__mm_tima = request.querystring("tima")
end if
%>
<%
dim sql
if recordset1__mm_sch="bbs_book" then
sql=" select * from bbs where bbsid in (select bbs_dist,bbs_id from bbs where " + replace(recordset1__mm_sch, "'", "''") + " like '%" + replace(recordset1__mm_text, "'", "''") + "%'and bbs_tima>=now()-" + replace(recordset1__mm_tima, "'", "''") + " and bbs_dist<>0 order by bbsid desc) order by bbsid desc"
else
sql="select * from bbs where " + replace(recordset1__mm_sch, "'", "''") + " like '%" + replace(recordset1__mm_text, "'", "''") + "%'and bbs_tima>=now()-" + replace(recordset1__mm_tima, "'", "''") + " and bbs_dist=0 order by bbsid desc"
end if
dim recordset1
dim recordset1_numrows
set recordset1 = server.createobject("adodb.recordset")
recordset1.activeconnection = mm_new_string
recordset1.source = sql
recordset1.cursortype = 0
recordset1.cursorlocation = 2
recordset1.locktype = 1
recordset1.open()
recordset1_numrows = 0
%>
<%
dim repeat1__numrows
dim repeat1__index
repeat1__numrows = 20
repeat1__index = 0
recordset1_numrows = recordset1_numrows + repeat1__numrows
%>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>用dw mx打造站内搜索</title>
</head>
<body>
<form name="form1" method="get" action="">
<p>关键词:
<input name="text" type="text" id="text" value="<%=request.querystring("text")%>">
选择条件:
<select name="select">
<option value="bbs_uesr+bbs_title" <%if (not isnull(request.querystring("select"))) then if ("bbs_uesr+bbs_title" = cstr(request.querystring("select"))) then response.write("selected") : response.write("")%>>不选定条件</option>
<option value="bbs_uesr" <%if (not isnull(request.querystring("select"))) then if ("bbs_uesr" = cstr(request.querystring("select"))) then response.write("selected") : response.write("")%>>作者</option>
<option value="bbs_title" <%if (not isnull(request.querystring("select"))) then if ("bbs_title" = cstr(request.querystring("select"))) then response.write("selected") : response.write("")%>>标题</option>
<option value="bbs_book" <%if (not isnull(request.querystring("select"))) then if ("bbs_book" = cstr(request.querystring("select"))) then response.write("selected") : response.write("")%>>回复内容</option>
</select>
<br>
选择时间:
<select name="tima" id="tima">
<option value="" <%if (not isnull(request.querystring("tima"))) then if ("" = cstr(request.querystring("tima"))) then response.write("selected") : response.write("")%>>不选定条件</option>
<option value="7" <%if (not isnull(request.querystring("tima"))) then if ("7" = cstr(request.querystring("tima"))) then response.write("selected") : response.write("")%>>一周前</option>
<option value="31" <%if (not isnull(request.querystring("tima"))) then if ("31" = cstr(request.querystring("tima"))) then response.write("selected") : response.write("")%>>一个月前</option>
<option value="182" <%if (not isnull(request.querystring("tima"))) then if ("182" = cstr(request.querystring("tima"))) then response.write("selected") : response.write("")%>>半年前</option>
<option value="365" <%if (not isnull(request.querystring("tima"))) then if ("365" = cstr(request.querystring("tima"))) then response.write("selected") : response.write("")%>>一年前</option>
</select>
<input type="submit" name="submit" value="搜索">
<br>
</form>
<table width="666" border="0" cellpadding="0" cellspacing="1" bgcolor="#ffcc33">
<tr bgcolor="#ffcc66">
<td width="291">标题</td>
<td width="156">作者</td>
<td width="219">发表时间</td>
</tr>
<%
while ((repeat1__numrows <> 0) and (not recordset1.eof))
%>
<tr bgcolor="#ffffff">
<td><%=replace((recordset1.fields.item("bbs_title").value),request.querystring("text"), "<font color='#ff0000'>"&request.querystring("text")&"</font>")%></td>
<td><%=(recordset1.fields.item("bbs_uesr").value)%></td>
<td><%=(recordset1.fields.item("bbs_tima").value)%></td>
</tr>
<%
repeat1__index=repeat1__index+1
repeat1__numrows=repeat1__numrows-1
recordset1.movenext()
wend
%>
</table>
</body>
</html>
<%
recordset1.close()
set recordset1 = nothing
%>
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器