日期:2013-03-15  浏览次数:20799 次

为了版美观,有时需要将一编较长的文章分页来显示,这时只好将文章分多次存入数据库,极不方便
本人见过多种自动分页代码,感觉上不是很理想
偶的思路是统计文章的所有行数,按指定行数输出显示内容并生成分页导航
如有不足之处望写信告诉作者

演示地址:http://mail-1.fz169.net/wh/sys/page/

<%
'#######################
'程序名:ASP文章自动分页
'编 写: 网 海 求 生 者
' Q Q:54883661 E-mail:
' wuyingke5155@163.com
'#######################


'连接数据库:
on error resume next
dim conn,connstr,dbpath
dbpath=server.mappath("web.mdb")'数据库文件名
set conn=server.createobject("adodb.connection")
connstr="driver={microsoft access driver (*.mdb)};dbq="&dbpath&";"
conn.open connstr
if err.number<>0 then
response.write err.description
err.clear
response.end
end if
sub connclose()
conn.close()
set conn=nothing
end sub

'读取数据:
dim rs,sql,conntent,title,id
id=1'trim(request("id")) '上页传来的ID值,为了调试方便此ID值临时赋为1
set rs=server.createobject("adodb.recordset")
sql="select * from news where id="&cint(id)
rs.open sql,conn,1,1
if not (rs.eof and rs.bof) then
content=rs("content")'读取内容
title=rs("title")'读取标题
end if
if err.number<>0 then
response.write err.description
err.clear
response.end
end if
rs.close
set rs=nothing
call connclose()

'分页处理部分:

'---------------------主代码开始--------------------------

dim page,pagecount,thispage,linenum,allline

const pageline=10'每页显示10行
linenum=split(content,"<br>") '本例为计算字符串<br>标记的个数
allline=ubound(linenum)+1'全文<br>(换行标记)总数
pagecount=int(allline\pageline)+1'计算总页数
page=request("page")
if isempty(page) then
thispage=1
else
thispage=cint(page)
end if
response.write "<title>"&title&"</title><b>"&title&"</b><hr>"
for i=0 to allline
if i+1>thispage*pageline-pageline and i<thispage*pageline then
response.write linenum(i) &"<br>"'输出分页后的内容
end if
next
response.write chr(13)&"<hr>"
response.write "<p align='center'>总共"&allline&"行 "&pagecount&"页 每页"&pageline&"行 "
for i=1 to pagecount
if thispage=i then
response.write i & " "
else
response.write "<a href='?page="&i&"&id="&id&"'>"&i&"</a> "'输出所有分页链接
end if
next
'---------------------主代码结束--------------------------
%>