用ASP做一個分頁程序
你是否為了你的站點文章過多而反復做鏈接上一頁、下一頁這些煩瑣的工作發愁呢?現在有了ASP,只要你的主頁支持ASP,你就可以很簡單的實現對文章的管理了,排序、鏈接、顯示這些工作都讓ASP去完成吧!這么輕松的主頁維護,你是不是也想試一試呢?
請看以下實現ASP分頁程序的代碼:
dim conn
dim connstr
dim totalPut
dim CurrentPage
dim TotalPages
dim i,j
dim sql
dim rs
on error resume next
'打開數據庫
connstr="DBQ="+server.mappath("book.mdb")+";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
set conn=server.createobject("ADODB.CONNECTION")
conn.open connstr
set rs=server.createobject("adodb.recordset")
'定義每頁文章顯示數
const MaxPerPage=18
if not isempty(request("page")) then
currentPage=cint(request("page"))
else
currentPage=1
end if
sql="select * from learning order by articleid desc"
Set rs= Server.CreateObject("ADODB.Recordset")
rs.open sql,conn,1,1
if rs.eof and rs.bof then
response.write "
else
'數據庫中文章數totalput
totalPut=rs.recordcount
if currentpage<1 then
currentpage=1
end if
'統計總頁數currentpage
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
showpage totalput,MaxPerPage,"index.asp"
showContent
showpage totalput,MaxPerPage,"index.asp"
else
if (currentPage-1)*MaxPerPage
rs.move (currentPage-1)*MaxPerPage
dim bookmark
bookmark=rs.bookmark
showpage totalput,MaxPerPage,"index.asp"
showContent
showpage totalput,MaxPerPage,"index.asp"
else
currentPage=1
showpage totalput,MaxPerPage,"index.asp"
showContent
showpage totalput,MaxPerPage,"index.asp"
end if
end if
rs.close
end if
set rs=nothing
conn.close
set conn=nothing
sub showContent
dim i
i=0
do while not rs.eof
>
'選擇顯示數據庫內容
"><=rs("title")>[點擊:<=rs("hits")>]
<
'當顯示記錄大于maxperpage時結束這頁
i=i+1
if i>=MaxPerPage then exit do
rs.movenext
loop
end sub
function showpage(totalnumber,maxperpage,filename)
'求出當每頁18篇文章時總共的頁數
dim n
if totalnumber mod maxperpage=0 then
n= totalnumber maxperpage
else
n= totalnumber maxperpage+1
end if
response.write ""
end function
>
以上代碼很簡單的就實現了ASP網頁的多種分頁功能,不論是對瀏覽者還是管理者都能很方便的瀏覽和管理,我只在相關部分作了一些解釋,象這個ASP程序還必須有數據庫的支持才可以,關于數據庫的問題我就不詳述了,具體的實例大家可以看看我的主頁(http://aasp.yeah.net)上的文章管理程序,另外還有具體的程序下載!
請看以下實現ASP分頁程序的代碼:
dim conn
dim connstr
dim totalPut
dim CurrentPage
dim TotalPages
dim i,j
dim sql
dim rs
on error resume next
'打開數據庫
connstr="DBQ="+server.mappath("book.mdb")+";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
set conn=server.createobject("ADODB.CONNECTION")
conn.open connstr
set rs=server.createobject("adodb.recordset")
'定義每頁文章顯示數
const MaxPerPage=18
if not isempty(request("page")) then
currentPage=cint(request("page"))
else
currentPage=1
end if
sql="select * from learning order by articleid desc"
Set rs= Server.CreateObject("ADODB.Recordset")
rs.open sql,conn,1,1
if rs.eof and rs.bof then
response.write "
還 沒 有 任 何 文 章
"else
'數據庫中文章數totalput
totalPut=rs.recordcount
if currentpage<1 then
currentpage=1
end if
'統計總頁數currentpage
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
showpage totalput,MaxPerPage,"index.asp"
showContent
showpage totalput,MaxPerPage,"index.asp"
else
if (currentPage-1)*MaxPerPage
rs.move (currentPage-1)*MaxPerPage
dim bookmark
bookmark=rs.bookmark
showpage totalput,MaxPerPage,"index.asp"
showContent
showpage totalput,MaxPerPage,"index.asp"
else
currentPage=1
showpage totalput,MaxPerPage,"index.asp"
showContent
showpage totalput,MaxPerPage,"index.asp"
end if
end if
rs.close
end if
set rs=nothing
conn.close
set conn=nothing
sub showContent
dim i
i=0
do while not rs.eof
>
'選擇顯示數據庫內容
"><=rs("title")>[點擊:<=rs("hits")>]
<
'當顯示記錄大于maxperpage時結束這頁
i=i+1
if i>=MaxPerPage then exit do
rs.movenext
loop
end sub
function showpage(totalnumber,maxperpage,filename)
'求出當每頁18篇文章時總共的頁數
dim n
if totalnumber mod maxperpage=0 then
n= totalnumber maxperpage
else
n= totalnumber maxperpage+1
end if
response.write ""
end function
>
以上代碼很簡單的就實現了ASP網頁的多種分頁功能,不論是對瀏覽者還是管理者都能很方便的瀏覽和管理,我只在相關部分作了一些解釋,象這個ASP程序還必須有數據庫的支持才可以,關于數據庫的問題我就不詳述了,具體的實例大家可以看看我的主頁(http://aasp.yeah.net)上的文章管理程序,另外還有具體的程序下載!