高手看过来,关于站内搜索结果分页显示及结果中突出显示搜索关键词的问题!

batistutafans 2006-04-05 02:22:37
今天弄一个站内搜索的程序,在CSDN上搜得一帖子:
http://community.csdn.net/Expert/topic/4199/4199802.xml?temp=.525448
其中“jspadmin(阿笨狗 http://www.pifoo.com 和朋友们一起进步)”的方法不错,我照搬就能解决问题,不过如果有满足搜索条件的记录的话是全部显示,我想实现分页显示,于是又找来一分页程序,合二为一,却出现了问题,具体情况如下:
搜索提交页search.asp
<form name="form1" method="post" action="result.asp" target="_blank">
<input name="key" type="text" id="key" size="16" maxlength="16">
<input name="Submit" type="image" src="images/index/sousuo.gif" width="21" height="21"/>
</form>

搜索结果页result.asp
<%
Dim strProvider,conn
strProvider="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
strProvider=strProvider & Server.MapPath("\") & "\database\beeyee.mdb" ''假设数据库存放在主页根目录下的data目录下
Set conn = Server.CreateObject("ADODB.connection")
conn.Open strProvider ''打开数据库连接
%>
<%
Dim S_Key,rs,StrSQL
S_Key = Trim(Request("key")) ''得到搜索关键字的值
If S_Key <>"" then
Set rs=Server.CreateObject("ADODB.RecordSet")
StrSQL=AutoKey(S_Key) ''此处使用自定义函数 AutoKey(),该函数为实现智能搜索的核心
rs.Open StrSQL,conn,3,2 ''得到搜索后的记录
page=1 ' 设置变量PAGE=1
rs.PageSize = 5 '每页显示记录数

if Not IsEmpty(Request("Page")) then '如果PAGE已经初始化...

Page = CInt(Request("Page")) '接收PAGE并化为数字型赋给PAGE变量

if Page > rs.PageCount then '如果接收的页数大于总页数
rs.AbsolutePage = rs.PageCount '设置当前显示页等于最后页

elseif Page <= 0 then '如果page小于等于0
Page = 1 '设置PAGE等于第一页
else
rs.AbsolutePage = Page '如果大于零,显示当前页等于接收的页数
end if
End if
Page = rs.AbsolutePage
%>
<%

For i = 1 to rs.PageSize
if rs.EOF then
Exit For
end if '利用for next 循环依次读出记录
%>
<%=rs("title")%><br>
<%rs.MoveNextnext%>
<%if request("page")>1 then%><a Href="result.asp?Page=<% = 1%>">首页</a> <a Href="result.asp?Page=<% =request("page") -1 %>">上一页</a><%end if %><%if request("page")<>rs.pagecount then %><a Href="result.asp?Page=<% =request("page") + 1%>">下一页</a> <a Href="result.asp?Page=<% = rs.PageCount%>">尾页</a> <% end if %>
<%
rs.close
Set rs = Nothing
%>
<%
Function AutoKey(strKey)
CONST lngSubKey=2
Dim lngLenKey, strNew1, strNew2, i, strSubKey

''检测字符串的合法性,若不合法则转到出错页。出错页你可以根据需要进行设定。
if InStr(strKey,"=")<>0 or InStr(strKey,"`")<>0 or InStr(strKey,"''")<>0 or InStr(strKey," ")<>0 or InStr(strKey," ")<>0 or InStr(strKey,"''")<>0 or InStr(strKey,chr(34))<>0 or InStr(strKey,"\")<>0 or InStr(strKey,",")<>0 or InStr(strKey,"<")<>0 or InStr(strKey,">")<>0 then
Response.write "对不起,请不要用非法字符进行搜索!"
End If

lngLenKey=Len(strKey)
Select Case lngLenKey
Case 0 ''若为空串,转到出错页
Response.write "对不起,没有找到满足搜索条件的记录!"
Case 1 ''若长度为1,则不设任何值
strNew1=""
strNew2=""
Case Else ''若长度大于1,则从字符串首字符开始,循环取长度为2的子字符串作为查询条件
For i=1 To lngLenKey-(lngSubKey-1)
strSubKey=Mid(strKey,i,lngSubKey)
strNew1=strNew1 & " or title like '%" & strSubKey & "%'"
strNew2=strNew2 & " or content like '%" & strSubKey & "%'"
Next
End Select

''得到完整的SQL语句
AutoKey="Select * from news where title like '%" & strKey & "%' or content like '%" & strKey & "%'" & strNew1 & strNew2

End Function
%>
<%
conn.close
set conn=nothing
%>
用于搜索的表news:
id int
title nvarchar
content ntext

如果用阿笨狗的方法,不分页显示的话,一切正常,如果加上分页,则显示结果为:
Microsoft VBScript 编译器错误 错误 '800a03ea'

语法错误

/result.asp,行106

Function AutoKey(strKey)

除了想结果这个分页显示的问题外,我还想在搜索结果里显示content的一部分内容(必须要包括这个搜索的关键字),并且把搜索的关键字在这个content中用红色表示,该如何解决呢?
...全文
682 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
libao1983 2006-04-06
  • 打赏
  • 举报
回复
而且分页可以使用另外方式 select top 10 * from table where id not in (select top 10*N id from table)
通过每次取top10,假设每页显示条数为10,每次取上次没有显示过的top 10来分页


我只会这样分页,传统的我都不会哈哈!
jspadmin 2006-04-06
  • 打赏
  • 举报
回复
而且分页可以使用另外方式 select top 10 * from table where id not in (select top 10*N id from table)
通过每次取top10,假设每页显示条数为10,每次取上次没有显示过的top 10来分页
jspadmin 2006-04-06
  • 打赏
  • 举报
回复
呵呵,楼主蛮有毅力的

但是我想只显示内容的前150个字符,改成
<%
content=Rs("content")
tmp1=replace(content,S_Key,"<font color='red'>" & S_Key & "</font>")
tmp2=replace(tmp1," ","") '去掉空格
tmp3=replace(tmp2,"<br>","") '去掉换行
if len(tmp3)>150 then
content=left(tmp3,150)
Response.write content
%>
batistutafans 2006-04-06
  • 打赏
  • 举报
回复
谢谢楼上的建议,我还是不要分页了,全部显示得了!
batistutafans 2006-04-06
  • 打赏
  • 举报
回复
to jspadmin:
你的代码里少了个end if,我加上了,写成下面的样子,但是从效果来看空格和换行为什么有的起作用了有的又没有呢?
<%
content=RST("content")
tmp1=replace(content,S_Key,"<font color='red'>" & S_Key & "</font>")
tmp2=replace(tmp1," ","") '去掉空格
tmp3=replace(tmp2,"<br>","") '去掉换行
if len(tmp3)>1500 then
content=left(tmp3,150)
Response.write content
end if
%>

关于分页的问题,现在觉得脑子有点大,对于大家的方法,不知道有没有最优的方案呢?假设数据量以后会很大,有几千条。
丛林蚂蚁 2006-04-06
  • 打赏
  • 举报
回复
此分页方法非常不建议,当数据量有几千条时.分页导航将会变得越来越慢.建议使用
page=request("page")
pagesize=30
select top pagesize * from table where 条件 and id not in(select top pagesize*(page-1) id from table where 条件)

注;如果page当前没值.请付值1.也就是说.PAGE的最小值是1
batistutafans 2006-04-06
  • 打赏
  • 举报
回复
呵呵,高人就是高啊,既然大家还是这么热心的帮助我,那我还是坚持把它彻底搞定吧!关于jspadmin的方法,我还真不知道该如何用呢!
libao1983 2006-04-05
  • 打赏
  • 举报
回复
我建议你好好看看asp的教程
batistutafans 2006-04-05
  • 打赏
  • 举报
回复
而且很奇怪,因为通常不用其他处理直接把内容取出来的话,它显示的会是所有字符的堆积,而不是自动按输入时的有空格就保留功课,有换行就换行,而在这里我没有这样做,显示的content竟然能自动进行空格和换行的处理,结果和输入时的一样,而我在这里偏偏要的不是这样,而是只要把前150个不为空的字符显示出来就行了的。
batistutafans 2006-04-05
  • 打赏
  • 举报
回复
不行啊,我写成了这样还是把content全部显示出来了
<%
content=Rs("content")
Response.write(Replace(content,S_Key,"<font color=red>" & S_Key & "</font>"))
content=left(content,150)
response.write (content)
%>
libao1983 2006-04-05
  • 打赏
  • 举报
回复
content=(left(content),150)

|
|
content=left(content,150)
batistutafans 2006-04-05
  • 打赏
  • 举报
回复
我按你的写成:
<%
content=Rs("content")
Response.write(Replace(content,S_Key,"<font color=red>" & S_Key & "</font>"))
content=(left(content),150)
response.write (content)
%>
结果提示:
Microsoft VBScript 编译器错误 错误 '800a03ee'

缺少 ')'

/result.asp,行105

content=(left(content),150)

batistutafans 2006-04-05
  • 打赏
  • 举报
回复
呵呵,楼上的,多谢,不过你这样写了后这个content显示出来就把我的页面撑开了啊,能不能只显示内容的前150个字符,并且不要按照内容原来的格式,而是把空格和换行都取消掉,全是字符的罗列呢?
libao1983 2006-04-05
  • 打赏
  • 举报
回复
content=(left(content),150)
libao1983 2006-04-05
  • 打赏
  • 举报
回复
Response.write (content):这一行不要

这样
Response.write(Replace(content,S_Key,"<font color=red>" & S_Key & "</font>"))
batistutafans 2006-04-05
  • 打赏
  • 举报
回复
呵呵,我太笨了,改了一下成功了
<%
content = Rs("content")
content=replace(content,S_Key,"<font color=""red"">" & S_Key & "</font>")
Response.write (content)
%>
但是我想只显示内容的前150个字符,改成
<%
content = Rs("content")
content=replace(content,S_Key,"<font color=""red"">" & S_Key & "</font>")
content=(left("content"),150)
Response.write (content)
%>
又出错了,
Microsoft VBScript 编译器错误 错误 '800a03ee'

缺少 ')'

/result.asp,行105

content=(left("content"),150)
------------------------^
该怎么改呢?
batistutafans 2006-04-05
  • 打赏
  • 举报
回复
关于显示内容中关键词突出显示的方法,我改为:<%=rs("title")%><br>
<%
content = Rs("content")
replace(content,S_Key,"<font color=""red"">" & S_Key & "</font>")
Response.write (content)
%>
结果说:
Microsoft VBScript 编译器错误 错误 '800a0414'

调用子程序时不能使用括号

/result.asp,行104

replace(content,S_Key,"<font color=""red"">" & S_Key & "</font>")

batistutafans 2006-04-05
  • 打赏
  • 举报
回复
我修改为
<%end if%>
<%
rs.close
Set rs = Nothing
conn.close
set conn=nothing
%>
就可以了,但是有一个问题,点下一页和最后页的时候说:
Microsoft VBScript 运行时错误 错误 '800a01a8'

缺少对象: ''

/result.asp,行137
而137行正是rs.close
这是怎么回事?
libao1983 2006-04-05
  • 打赏
  • 举报
回复
content = Rs("content")
replace(content,searchword,"<font color=""red"">" & searchword & "</font>")
Response.write (content)

这样输出加粗关键字的片断
batistutafans 2006-04-05
  • 打赏
  • 举报
回复
单独的分页程序没有问题,以下调试通过:
<%
Dim strProvider,conn
strProvider="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
strProvider=strProvider & Server.MapPath("\") & "\database\beeyee.mdb" ''假设数据库存放在主页根目录下的data目录下
Set conn = Server.CreateObject("ADODB.connection")
conn.Open strProvider ''打开数据库连接

Set rs = Server.CreateObject ("ADODB.Recordset")
sql="select * from news order by id desc"
rs.Open sql, conn, 1

page=1 ' 设置变量PAGE=1
rs.PageSize = 20 '每页显示记录数

if Not IsEmpty(Request("Page")) then '如果PAGE已经初始化...

Page = CInt(Request("Page")) '接收PAGE并化为数字型赋给PAGE变量

if Page > rs.PageCount then '如果接收的页数大于总页数
rs.AbsolutePage = rs.PageCount '设置当前显示页等于最后页

elseif Page <= 0 then '如果page小于等于0
Page = 1 '设置PAGE等于第一页
else
rs.AbsolutePage = Page '如果大于零,显示当前页等于接收的页数
end if
End if
Page = rs.AbsolutePage
%>
<%

For i = 1 to rs.PageSize
if rs.EOF then
Exit For
end if '利用for next 循环依次读出记录
%>
<table width=500 border=1 align=center><tr><TD><% =rs("title") %>
</td>
</tr>
</table>
<%rs.MoveNext
next%>
<%if request("page")>1 then%><a Href="fenye.asp?Page=<% = 1%>">首页</a> <a Href="fenye.asp?Page=<% =request("page") -1 %>">上一页</a><%end if %><%if request("page")<>rs.pagecount then %><a Href="fenye.asp?Page=<% =request("page") + 1%>">下一页</a> <a Href="fenye.asp?Page=<% = rs.PageCount%>">尾页</a> <% end if %>
而综合起来的总是出现错误:
Microsoft VBScript 编译器错误 错误 '800a03f6'

缺少 'End'

/result.asp,行138
我改了后138行附近代码为:
<%
rs.close
Set rs = Nothing
conn.close
set conn=nothing
%>
加载更多回复(5)

28,408

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧