asp用access数据库实现留言板分页的问题
我现在在做一个留言板,什么都做好了,就差分页了。用的Acess数据库建了个叫name.mdb的数据库,留言内容放在表messages里,下边有自动编号,昵称,留言内容,留...
我现在在做一个留言板,什么都做好了,就差分页了。用的Acess数据库建了个叫name.mdb的数据库,留言内容放在表messages里,下边有自动编号,昵称,留言内容,留言时间(date),需要实现分页功能,我在网上找了段代码,但是不知道怎么样,谁帮我解释下这段代码怎么用,谢谢
<% const MaxPerPage=4 '单独页最大记录数 const 用来申明常量
dim sql
dim rs
dim totalPut '总记录
dim CurrentPage '当前页次
dim TotalPages '总页数
dim i
set conn=server.createobject("adodb.connection")
connstr="Provider=Microsoft.jet.oledb.4.0;data source="&server.mappath("?/.mdb")
conn.open connstr
set rs=server.CreateObject("adodb.recordset")
sql="select * from [?] order by id desc"
rs.open sql,conn,1,2
%>
<%
rs.MoveFirst '注意放到前面来,否则到任何页总是在第一个记录上
rs.pagesize=MaxPerPage '设置每页最多显示多少条记录
If trim(Request("Page"))<>"" then '如果请求的页次不为空
CurrentPage= CLng(request("Page")) 'clng是转换成长整型数据类型,并赋值到当前页次上
If CurrentPage> rs.PageCount then '如果当前页次大于总页数,则将最大页次赋值到当前页次上
CurrentPage = rs.PageCount
End If
Else
CurrentPage= 1 '一切条件不成立,将当前页设为第一页
End If
totalPut=rs.recordcount '将总记录赋值于TOTALPUT
if CurrentPage<>1 then '如果当前页数不等于第一页
if (currentPage-1)*MaxPerPage<totalPut then '如果当前页减一乘以每页最大的记录数小于总记录的话
rs.move(currentPage-1)*MaxPerPage '相对当前记录数向后移动
dim bookmark '定义书签变量
bookmark=rs.bookmark '将当前记录的标签赋于变量BOOKMARK上
end if
end if
dim n,k
if (totalPut mod MaxPerPage)=0 then '总记录数与每页最大记录数求余的结果为零时,则N返回整数页次,否则再加一.
n= totalPut \ MaxPerPage
else
n= totalPut \ MaxPerPage + 1
end if
%>
<%
i=0
Do While Not rs.EOF and i<maxperpage
%>
为要循环的块内容
<%
i=i+1
rs.MoveNext
Loop'结束分页内容并加以循环。分页显示代码块 可独立使用,注意和上面分页功能代码配使用
%>
当前第<%=currentpage%>页 总共<%=n%>页
<%k=currentPage
if k<>1 then
response.write "[<b>"+"<a href='indexbbs.asp?page=1'>首页</a></b>] "
response.write "[<b>"+"<a href='indexbbs.asp?page="&cstr(k-1)&"'>上一页</a></b>] "
else
Response.Write "[首页] [上一页]"
end if
if k<>n then
response.write "[<b>"+"<a href='indexbbs.asp?page="&cstr(k+1)&"'>下一页</a></b>] "
response.write "[<b>"+"<a href='indexbbs.asp?page="&cstr(n)&"'>尾页</a></b>] "
else
Response.Write "[下一页] [尾页]"
end if
%> 展开
<% const MaxPerPage=4 '单独页最大记录数 const 用来申明常量
dim sql
dim rs
dim totalPut '总记录
dim CurrentPage '当前页次
dim TotalPages '总页数
dim i
set conn=server.createobject("adodb.connection")
connstr="Provider=Microsoft.jet.oledb.4.0;data source="&server.mappath("?/.mdb")
conn.open connstr
set rs=server.CreateObject("adodb.recordset")
sql="select * from [?] order by id desc"
rs.open sql,conn,1,2
%>
<%
rs.MoveFirst '注意放到前面来,否则到任何页总是在第一个记录上
rs.pagesize=MaxPerPage '设置每页最多显示多少条记录
If trim(Request("Page"))<>"" then '如果请求的页次不为空
CurrentPage= CLng(request("Page")) 'clng是转换成长整型数据类型,并赋值到当前页次上
If CurrentPage> rs.PageCount then '如果当前页次大于总页数,则将最大页次赋值到当前页次上
CurrentPage = rs.PageCount
End If
Else
CurrentPage= 1 '一切条件不成立,将当前页设为第一页
End If
totalPut=rs.recordcount '将总记录赋值于TOTALPUT
if CurrentPage<>1 then '如果当前页数不等于第一页
if (currentPage-1)*MaxPerPage<totalPut then '如果当前页减一乘以每页最大的记录数小于总记录的话
rs.move(currentPage-1)*MaxPerPage '相对当前记录数向后移动
dim bookmark '定义书签变量
bookmark=rs.bookmark '将当前记录的标签赋于变量BOOKMARK上
end if
end if
dim n,k
if (totalPut mod MaxPerPage)=0 then '总记录数与每页最大记录数求余的结果为零时,则N返回整数页次,否则再加一.
n= totalPut \ MaxPerPage
else
n= totalPut \ MaxPerPage + 1
end if
%>
<%
i=0
Do While Not rs.EOF and i<maxperpage
%>
为要循环的块内容
<%
i=i+1
rs.MoveNext
Loop'结束分页内容并加以循环。分页显示代码块 可独立使用,注意和上面分页功能代码配使用
%>
当前第<%=currentpage%>页 总共<%=n%>页
<%k=currentPage
if k<>1 then
response.write "[<b>"+"<a href='indexbbs.asp?page=1'>首页</a></b>] "
response.write "[<b>"+"<a href='indexbbs.asp?page="&cstr(k-1)&"'>上一页</a></b>] "
else
Response.Write "[首页] [上一页]"
end if
if k<>n then
response.write "[<b>"+"<a href='indexbbs.asp?page="&cstr(k+1)&"'>下一页</a></b>] "
response.write "[<b>"+"<a href='indexbbs.asp?page="&cstr(n)&"'>尾页</a></b>] "
else
Response.Write "[下一页] [尾页]"
end if
%> 展开
展开全部
'例如文件名为a.asp
<%'开始分页
Const MaxPerPage=20 '每页显示多少条
dim totalPut
dim CurrentPage
dim TotalPages
dim j
dim sql
if Not isempty(request("page")) then
currentPage=Cint(request("page"))
else
currentPage=1
end if
set rs=server.createobject("adodb.recordset")
rs.open "select * from 表",conn,1,1
if err.number<>0 then
response.write "数据库中无数据"
end if
if rs.eof And rs.bof then
Response.Write "<p align='center'> 您还没有添加!</p>"
else
totalPut=rs.recordcount
if currentpage<1 then
currentpage=1
end if
if (currentpage-1)*MaxPerPage>totalput then
if (totalPut mod MaxPerPage)=0 then
currentpage= totalPut \ MaxPerPage
else
currentpage= totalPut \ MaxPerPage + 1
end if
end if
if currentPage=1 then
showContent
showpage totalput,MaxPerPage,"a.asp"'修改为你的页面名称
else
if (currentPage-1)*MaxPerPage<totalPut then
rs.move (currentPage-1)*MaxPerPage
dim bookmark
bookmark=rs.bookmark
showContent
showpage totalput,MaxPerPage,"a.asp"'修改为你的页面名称
else
currentPage=1
showContent
showpage totalput,MaxPerPage,"a.asp"'修改为你的页面名称
end if
end if
end if
sub showContent
dim i
i=0
%>
<table width="95%" border="1" align="center" cellpadding="0" cellspacing="2" bordercolorlight="#B0C8EA" bordercolordark="#FFFFFF" bgcolor="#FFFFFF">
<tr bgcolor="#f1f1f1">
<td width="9%" bgcolor="#D7E8F8"><div align="center">编号</a></div></td>
</tr>
<%do while not rs.eof%>
<tr bgcolor="#FFFFFF">
<td><div align="center"><%=rs("bianhao")%></div></td>
</tr>
<%i=i+1
if i>=MaxPerPage then Exit Do
rs.movenext
loop
rs.close
set rs=nothing%>
</table>
<%
End Sub
Function showpage(totalnumber,maxperpage,filename)
Dim n
If totalnumber Mod maxperpage=0 Then
n= totalnumber \ maxperpage
Else
n= totalnumber \ maxperpage+1
End If
Response.Write "<form method=Post action="&filename&">"
Response.Write "<p align='center'> "
If CurrentPage<2 Then
Response.Write "首页 上一页 "
Else
Response.Write "<a href="&filename&"?page=1>首页</a> "
Response.Write "<a href="&filename&"?page="&CurrentPage-1&">上一页</a> "
End If
If n-currentpage<1 Then
Response.Write "下一页 尾页"
Else
Response.Write "<a href="&filename&"?page="&(CurrentPage+1)&">"
Response.Write "下一页</a> <a href="&filename&"?page="&n&">尾页</a>"
End If
Response.Write " 页次:<font color='blue'>"&CurrentPage&"</font>/"&n&"页</font> "
Response.Write " 共有<font color='blue'>"&totalnumber&"</font>条信息 "
Response.Write "转到:<input type='text' name='page' size=2 maxlength=10 value="¤tpage&">页"
Response.Write " <input type='submit' value='GO' class='go-wenbenkuang' name='cndok'></form>"
End Function
%>
</td>
</tr>
</table>
<%'开始分页
Const MaxPerPage=20 '每页显示多少条
dim totalPut
dim CurrentPage
dim TotalPages
dim j
dim sql
if Not isempty(request("page")) then
currentPage=Cint(request("page"))
else
currentPage=1
end if
set rs=server.createobject("adodb.recordset")
rs.open "select * from 表",conn,1,1
if err.number<>0 then
response.write "数据库中无数据"
end if
if rs.eof And rs.bof then
Response.Write "<p align='center'> 您还没有添加!</p>"
else
totalPut=rs.recordcount
if currentpage<1 then
currentpage=1
end if
if (currentpage-1)*MaxPerPage>totalput then
if (totalPut mod MaxPerPage)=0 then
currentpage= totalPut \ MaxPerPage
else
currentpage= totalPut \ MaxPerPage + 1
end if
end if
if currentPage=1 then
showContent
showpage totalput,MaxPerPage,"a.asp"'修改为你的页面名称
else
if (currentPage-1)*MaxPerPage<totalPut then
rs.move (currentPage-1)*MaxPerPage
dim bookmark
bookmark=rs.bookmark
showContent
showpage totalput,MaxPerPage,"a.asp"'修改为你的页面名称
else
currentPage=1
showContent
showpage totalput,MaxPerPage,"a.asp"'修改为你的页面名称
end if
end if
end if
sub showContent
dim i
i=0
%>
<table width="95%" border="1" align="center" cellpadding="0" cellspacing="2" bordercolorlight="#B0C8EA" bordercolordark="#FFFFFF" bgcolor="#FFFFFF">
<tr bgcolor="#f1f1f1">
<td width="9%" bgcolor="#D7E8F8"><div align="center">编号</a></div></td>
</tr>
<%do while not rs.eof%>
<tr bgcolor="#FFFFFF">
<td><div align="center"><%=rs("bianhao")%></div></td>
</tr>
<%i=i+1
if i>=MaxPerPage then Exit Do
rs.movenext
loop
rs.close
set rs=nothing%>
</table>
<%
End Sub
Function showpage(totalnumber,maxperpage,filename)
Dim n
If totalnumber Mod maxperpage=0 Then
n= totalnumber \ maxperpage
Else
n= totalnumber \ maxperpage+1
End If
Response.Write "<form method=Post action="&filename&">"
Response.Write "<p align='center'> "
If CurrentPage<2 Then
Response.Write "首页 上一页 "
Else
Response.Write "<a href="&filename&"?page=1>首页</a> "
Response.Write "<a href="&filename&"?page="&CurrentPage-1&">上一页</a> "
End If
If n-currentpage<1 Then
Response.Write "下一页 尾页"
Else
Response.Write "<a href="&filename&"?page="&(CurrentPage+1)&">"
Response.Write "下一页</a> <a href="&filename&"?page="&n&">尾页</a>"
End If
Response.Write " 页次:<font color='blue'>"&CurrentPage&"</font>/"&n&"页</font> "
Response.Write " 共有<font color='blue'>"&totalnumber&"</font>条信息 "
Response.Write "转到:<input type='text' name='page' size=2 maxlength=10 value="¤tpage&">页"
Response.Write " <input type='submit' value='GO' class='go-wenbenkuang' name='cndok'></form>"
End Function
%>
</td>
</tr>
</table>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询