"定義每頁最大文章標題顯示量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 %>