当前位置: 首页 > 图文教程 > 网络编程 > ASP > ASP文章系统解决方案实现上一页下一页

ASP
asp实现取得数组中的最大值的代码
asp 将日期格式化为需要的格式
asp 下产生任意位数随机密码的代码
asp下返回以千分位显示数字格式化的数值
asp重定向页面的方法总结
asp下去除数组中重复的项的方法
asp下实现 重新排序数字数组的代码
asp 验证输入网址是否有效并可以访问 与正则验证输入网址
asp验证Ip格式的函数
asp实现生成由数字,大写字母,小写字母指定位数的随机数
asp 格式化sql中的like字符串
asp 实现显示所有的服务器变量值的函数
asp 取得用户真实IP,对代理地址仍然有效的函数
asp 通用数据库连接过程函数
asp 字符串截取函数
asp下实现格式化文件大小以MB显示的函数
asp 下用正则表达式检测邮箱格式的函数
asp 实现检测字符串是否为纯字母和数字组合的函数
asp代码实现检测组件是否安装的函数
asp通过JMAIL实现通用发送函数

ASP文章系统解决方案实现上一页下一页


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 127 ::
收藏到网摘: n/a

首先感谢V37斑竹对我的帮助,这个方案解决了显示“上一篇下一篇”和相关文章的问题,贴出来让大家分享。
以前看到一个帖子讲用ID+1和ID-1的办法判断“上一篇下一篇”,在用的过程中发现一个问题:当删除数据库中的一篇文章时,就会造成ID不连续,如果用ID+1和ID-1来判断就会出现找不到记录的问题,在这个程序里,通过查询大于当前ID的第一条记录来找出下一篇的ID,查询小于当前ID的第一条记录来找出上一篇的ID,这样就算ID不连续也可以正常显示了。
至于相关文章的显示则是在数据表里添加一个boardid字段来区分不同的文章栏目,在每次添加一篇新文章时加上boardid号就可以了,显示一篇文章时根据boardid来查询数据库就能显示出相关文章。
数据表articles中的字段有id,boardid,title,content,author,addtime
复制代码 代码如下:

<!--程序开始-->
'定义一个thenext函数来找出下一篇的ID,如果当前记录已经是最后一条记录,则输出文字“没有了”
<%
function thenext
newrs=server.CreateObject("adodb.recordset")
sql="select top 1 * from articles where id>"&a1&" order by id"
set newrs=conn.execute(sql)
if newrs.eof then
response.Write("没有了")
else
a2=newrs("id")
response.Write("<a href='view.asp?id="&a2&"'>下一篇</a>")
end if
end function
%>
'定义一个thehead函数来找出下一篇的ID,如果当前记录已经是最前面的一条记录,则输出文字“没有了”
<%
function thehead
headrs=server.CreateObject("adodb.recordset")
sql="select top 1 * from articles where id<"&a1&" order by id desc"
set headrs=conn.execute(sql)
if headrs.eof then
response.Write("没有了")
else
a0=headrs("id")
response.Write("<a href='view.asp?id="&a0&"'>上一篇</a>")
end if
end function
%>
'数据库连接文件
<!--#include file="conn.asp"-->
'取得传递过来的ID,显示文章标题作者和内容
<%
id=request("id")
sql="select * from articles where id="&id
set rs=conn.execute(sql)
%>
<% boardid=rs("boardid") %>
<title>文章系统-<% =rs("title") %></title><body leftmargin="0" topmargin="0">
<!--#include file="top.asp" -->
<%
Do While Not rs.EOF
%>
<table width="773" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td width="576" align="left">
<table width="557" border="0" cellspacing="5" cellpadding="4" align="left">
<tr>
<td colspan="2" align="center"><span style="font-size:9pt color:#efefef"><%= rs("title") %><br>
<div align="right"><span style="font-size:9pt color:#efefef">作者:<%= rs("author") %></span></div>
</span></td>
</tr>
<tr>
<td colspan="2" ><span style="font-size:9pt color:#efefef"><!--将数据库的资料取出,经过编码后输出,保持输入时的格式不变--><%= replace(server.HTMLEncode(rs("content")),chr(13),"<br>") %></span></td>
</tr>
<% a1=rs("id") %>
<tr>
<td width="269" align="right"><!--调用前面定义的显示上一篇的函数--><% thehead %></td>
<td width="257" align="right"><!--调用前面定义的显示下一篇的函数--><% thenext %></td>
</tr>
<% rs.MoveNext%>
<%Loop%>
</table></td>
<td width="217" valign="top" align="left">相关文章:
'根据当前文章的栏目号,找出同一栏目的文章
<%
sql="select * from articles where boardid="&boardid&""
set rs=conn.execute(sql)
%>
<%
Do While Not rs.EOF
%>
<table width="207" border="0" cellspacing="2" cellpadding="2">
<tr>
<td height="20"><a href="view.asp?id=<%=rs("id")%>"><%= rs("title") %></a></td>
</tr>
</table>
<% rs.MoveNext%>
<%Loop%>
</td>
</tr>
</table>
<!--#include file="copyright.asp" -->
</body>
<!--程序结束-->


复制代码 代码如下:

写的不错哦,我在网站上也是这么用的,不过没这个好,那个表里有1百多万数据,查一下就出来几万。只能这样搞。
顺便贴一个:学习一下:
---------------------------------
一种理论上最快的Web数据库分页方法
来自:新闻中心 
点击:2 加入时间:2002-11-22 15:53:41
上篇我们谈到了关于数据库传统的三种分页方法和他们的利弊,并且提出了一种理论上最佳的分页方法,本篇我们就来详细说说这种最佳的分页方法。
一:构思。
在设计Web数据库时,如果我们要编历每一条纪录,那么只有采取分页模式才可以使Web数据库尽快,尽好的呈现给终端用户,也不会因为8秒原则而使用户失去浏览该页的兴趣。但是即使采取分页的办法,当出现多纪录的数据库时,也难免会使我们的用户感到翻页时速度太慢。就如同我的上篇文章说的那样,几乎上一片文章的三种分页方法都有一些缺陷。那么,我们如何做到能让数据库每次就取我们需要的纪录,这个很好实现,有游标返回多个纪录集就可以实现,但是如果让数据库的一端不会因为要刚好检索一页的纪录而大耗资源就很难了。最后,经过我的不断改写程序与测试,终于编写出了我认为理论上最快的Web数据库分页方法。
二:具体实现的存储过程。
我们结合一个BBS问题来谈谈这种方法。如何让一个BBS每次每页只现实需要的一页纪录呢?而我们需要提供给数据库有那些参数呢?可能会有以下参数。
第一:就是我们需要的当前的页数。
第二:当前定义的每一页的纪录集数目。这样你就可以根据需要在页面程序中修改每一页的纪录数。当然,如果你不考虑程序的可扩展性,你也可以在数据库里直接规定每一页有N条纪录即可。
第三:一个输出参数:就是从数据库里得出当前表中总纪录数目的多少。(注意,他不是一个页的纪录数目)他相当于ADO分页法中的Recordcount。如果你不需要总纪录数目可以不用返回他。
我们来看具体存储过程的代码。。。
CREATE PROCEDURE dbo.PRO_pageview
(
@tint_tableid tinyint=1, --这个是BBS的当前版面Id,你可以不用管他。。
@int_pagenow int=0,
@int_pagesize int=0,
@int_recordcount int=0 output --就是得出BBS某个版面的总贴数。。
)
AS
set nocount on
declare @int_allid int
declare @int_beginid int,@int_endid int
declare @int_pagebegin int, @int_pageend int
select @int_allid=count(*) from tab_discuss where tint_level=0 and tint_tableid=@tint_tableid
select @int_recordcount=@int_allid --得出该版面的总贴数
declare cro_fastread cursor scroll
for select int_id from tab_discuss where tint_level=0 and tint_tableid=@tint_tableid order by int_id desc --这里定义游标操作,但是不用临时纪录集,而且游标也不需要全部遍历所有纪录集。
open cro_fastread --打开游标
select @int_beginid=(@int_pagenow-1)*@int_pagesize+1 得出该页的第一个纪录Id
select @int_endid = @int_beginid+@int_pagesize-1 得出该页的最后一个纪录的Id
fetch absolute @int_beginid from cro_fastread into @int_pagebegin 将他的Id传给一个变量该页开始的Id
if @int_endid>@int_allid --这里要注意,如果某一页不足固定页数的纪录时。如只有一页纪录,而且纪录少于我们定义的数目。或者是最后一页时。。。
fetch last from cro_fastread into @int_pageend --直接将游标绝对定位到最后一条纪录,得出他的id号来。。。
else
fetch absolute @int_endid from cro_fastread into @int_pageend
select int_id,tint_level,tint_children,var_face,var_subject,datalength(txt_content) as int_len,sint_hits,var_url,var_image,var_user,dat_time,tint_tableid,bit_kernul from tab_discuss where tint_tableid=@tint_tableid and int_rootid between @int_pageend and @int_pagebegin order by int_rootid desc,num_order desc --我们就可以利用该页的第一个id和最后一个id得出中间的id来。。。。(注意。我们这个BBS的数性结构用了一种很巧妙的算法,就是用一个orderNum浮点数即可完成排序。。。)
--开始清场。。。
close cro_fastread
deallocate cro_fastread
return
我们再看看Asp页面里的程序操作。。。
pagenow=cint(request("pagenow")) --当前的页面。
if pagenow<=0 then pagenow=1
pagesize=10
set cmd=server.CreateObject("adodb.command")
cmd.ActiveConnection=strconn
cmd.CommandType=4
cmd.CommandText="pro_pageview"
cmd.Parameters.Append cmd.CreateParameter("tint_tableid",adInteger,adParamInput,,tint_tableid)
cmd.Parameters.Append cmd.CreateParameter("int_pagenow",adInteger,adParamInput,,pagenow)
cmd.Parameters.Append cmd.CreateParameter("int_pagesize",adInteger,adParamInput,,pagesize)
cmd.Parameters.Append cmd.CreateParameter("int_recordcount",adInteger,adParamOutput)
set rs=cmd.Execute
if rs.eof then
Response.Write "目前已超过了纪录个数或纪录集为空!"
Response.End
end if
dim arrRs
arrRs=rs.getrows '可以利用getRows将纪录集迅速保存到一个二维的数组中来提高速度。
recordcount=cmd.Parameters("int_recordcount")
'注意,当纪录不足以刚好整除单位页纪录时,我们也要将其定义为一页,如纪录数目为2页多一个纪录,此时我们的页数也要为3页纪录。
if (recordcount mod pagesize)=0 then
pagecount=recordcount\pagesize
else
pagecount=recordcount\pagesize+1
end if
<--分页开始 -->
<!-- #include file="include\tablepage.asp" -->固定的分页函数,其实无非是pagenow+1或pagenow-1,pagenow,pagecount
<!--分页结束-->
<div align="left" class="pblank" >
<%
'---------显示树性结构!-------------
level=0
Response.Write "<ul>"
for i=0 to ubound(arrRs,2)
if arrRs(1,i)>level then
Response.Write "<ul>"
end if
if arrRs(1,i)<level then
for j=arrRs(1,i) to level-1
Response.Write "</ul>"
next
end if
int_size=arrRs(5,i)
if int_size=0 then
str_size=" <

"