"定義每頁最大文章標題顯示量MaxPerPage,你可以自己修改這裡的數字來達到你的最佳顯示效果
const MaxPerPage=18
dim totalPut
dim CurrentPage
dim TotalPages
dim i,j
"假如返回的頁面資訊是空的,也就是如果你直接輸入index.asp,那麼就用這裡定義的頁數第一頁
if not isempty(request("page")) then
currentPage=cint(request("page"))
else
currentPage=1
end if
dim sql
dim rs
dim rstype
dim typesql
dim typeid,typename
"如果返回的欄目資訊為空白,那麼就用這裡定義的欄目,這裡指定的是第三個欄目
if not isEmpty(request("typeid")) then
typeid=request("typeid")
else
typeid=3
end if
"通過返回的欄目typeid號,開啟資料庫顯示指定的欄目,並把其值交給typename
set rstype=server.createobject("adodb.recordset")
typesql="select * from type where typeID="&cstr(typeid)
rstype.open typesql,conn,1,1
typename=rstype("type")
rstype.close
%>
"開啟指定的記錄集article並按照文章的加入日期排序,在這裡開啟有兩個條件,一個是利用like來查詢資料庫並顯示相關文章標題,還有就是通過返回的typeid顯示指定欄目的文章
<%
sql="select * from article where title like '%"&request("txtitle")&"%' and typeid="+cstr(typeid)+" order by date desc"
Set rs= Server.CreateObject("ADODB.Recordset")
rs.open sql,conn,1,1
"如果查詢資料庫的結果指向記錄集的開始或者結尾,表示資料庫中沒有任何相關文章
if rs.eof and rs.bof then
response.write "<p align='center'>沒有或沒有找到任何文章</p>"
else
"如果資料庫內有內容,則取得資料庫內文章數目
totalPut=rs.recordcount
"假如頁面參數currentpage小於1,則指定為1
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
"如果分頁的頁數為1或者頁面數減1乘與頁面最大文章數小於文章總數,則用已經做好的function showpage在showContent子程式也就是顯示文章標題部分的上面和下面顯示分頁程式
if currentPage=1 then
showpage totalput,MaxPerPage,"index.asp"
showContent
showpage totalput,MaxPerPage,"index.asp"
else
if (currentPage-1)*MaxPerPage<totalPut then
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
"顯示文章標題及相關資料庫內容子程式
sub showContent
dim i
i=0
%>
<div align="center"><center><table border="1" cellspacing="0" width="589" bordercolorlight="#000000" bordercolordark="#FFFFFF" cellpadding="0">
<tr>
<td width="46" align="center" bgcolor="#D0D0D0" height="20"><strong>ID號</strong></td>
<td width="381" align="center" bgcolor="#D0D0D0"><strong>文章標題</strong></td>
<td width="98" align="center" bgcolor="#D0D0D0"><strong>加入日期</strong></td>
<td width="58" align="center" bgcolor="#D0D0D0"><strong>點擊</strong></td>
</tr>
<%do while not rs.eof%>
<tr>
"依次顯示文章ID號,文章標題,文章加入日期及瀏覽數,這裡的openarticle.asp是用來重新定向文章的,以後將為大家介紹
<td height="23" width="46"><p align="center"><%=rs("articleid")%></td>
<td width="381"><p align="center"><a href="javascript:popwin2('openarticle.asp?id=<%=rs("articleid")%>&typeid=<%=cstr(typeid)%>')"><%=rs("title")%></a></td>
<td width="98"><p align="center"><font color="red"><i><%=rs("date")%></i></font></td>
<td width="58"><p align="center"><%=rs("hits")%></td>
</tr>
<%
"這裡是一個迴圈,每顯示一篇文章,則定義的變數i的值加一,當i的值大於或等於頁面最大文章數時退出迴圈
i=i+1
if i>=MaxPerPage then exit do
"顯示完一篇文章以後,自動移到記錄集的下一個記錄
rs.movenext
loop
%>
</table>
</center></div><%
end sub
"顯示分頁的function
function showpage(totalnumber,maxperpage,filename)
dim n
"利用文章數算出文章的分頁數N
if totalnumber mod maxperpage=0 then
n= totalnumber \ maxperpage
else
n= totalnumber \ maxperpage+1
end if
response.write "<form method=Post action="&filename&"?typeid="&typeid&">"
response.write "<p align='center'><font color='#000080'>>>分頁</font> "
"如果當前頁數小於2,則顯示的文章首頁和上一頁不顯示串連,否則用當前頁數減去1來顯示上一頁,直接用page=1來顯示首頁
if CurrentPage<2 then
response.write "<font color='#000080'>首頁 上一頁</font> "
else
response.write "<a href="&filename&"?page=1&typeid="&typeid&">首頁</a> "
response.write "<a href="&filename&"?page="&CurrentPage-1&"&typeid="&typeid&">上一頁</a> "
end if
"假如分頁頁數小於1,則直接顯示下一頁和尾頁,否則用當前頁數加上1來顯示下一頁,用已經算出的文章分頁數N顯示文章的尾頁
if n-currentpage<1 then
response.write "<font color='#000080'>下一頁 尾頁</font>"
else
response.write "<a href="&filename&"?page="&(CurrentPage+1)&"&typeid="&typeid&">"
response.write "下一頁</a> <a href="&filename&"?page="&n&">尾頁</a>"
end if
<%@ LANGUAGE="VBSCRIPT" %>
"開啟資料庫連接
<!--#include file="conn.asp"-->
<%response.buffer=false
dim sql
dim rs
dim articleid
"利用update從文章串連處返回的文章號ID更新指定文章的瀏覽數,以及利用response.redirect重新導向文章的串連
articleid=request("id")
set rs=server.createobject("adodb.recordset")
sql="update article set hits=hits+1 where articleID="&articleid
rs.open sql,conn,1,1
rs.close
conn.close
response.redirect "list.asp?id="&articleid
%>