ASP進階之文章線上管理更新(8)

來源:互聯網
上載者:User
 經過了文章的添加、儲存、顯示,那麼現在應該來談談關於管理程式的顯示首頁面,也就是顯示所有文章的標題串連,以方便瀏覽者尋找文章,其應該具有的功能有:顯示所有文章的標題串連,加入日期,瀏覽次數等資訊,另外還必須提供分頁功能,要不然這麼多的文章標題在一個頁面都顯示出來,那將非常的費時且不便瀏覽,另外由於本程式在這裡結合了文章分欄目搜尋的功能,所以在這裡也將一起介紹了。

   下面就為大家詳細的介紹首頁面index.asp的這些功能的實現過程以及其具體功用:

   "建立資料庫連接
<!--#include file="conn.asp"-->
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>ASP專題欄目</title>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
<link rel="stylesheet" type="text/css" href="style.css">
<script language="javascript">
function popwin2(path)
{ window.open(path,"","height=450,width=600,resizable=yes,scrollbars=yes,status=no,toolbar=no,menubar=no,location=no");
}
</script>
</head>
<%

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

<body>
<div align="center"><center>

<table border="0" width="95%" cellspacing="0" cellpadding="0">
  <tr>
    <td width="100%" style="border-left: thin dotted rgb(0,128,0); border-right: thin dotted rgb(0,128,0)"><p align="center"><br>

   "顯示欄目資訊,當你點擊了任何一個欄目,在typename的位置都會顯示相關資訊,這裡沒有採用欄目的自動顯示方式是考慮了頁面的顯示效果,而採用手工添加的模式,要想採用自動模式,就自己寫吧:)相信你學習到現在,不會連這個都編不出來吧!
    動網ASP技巧專題>><font color="#FF0000"><%response.write ""&typename&""%></font><br>
    <a href="index.asp?typeid=1">ASP FAQ</a>|<a href="index.asp?typeid=2">ASP組件</a> |<a href="index.asp?typeid=3">ASP文摘</a>|<a href="index.asp?typeid=4">ASP執行個體</a>|<a href="index.asp?typeid=5">ASP安全</a> </p>
    <div align="center">

   "開啟指定的記錄集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

    "用N和maxperpage顯示文章的分頁數和每頁的文章數
   response.write "<font color='#000080'> 頁次:</font><strong><font color=red>"&CurrentPage&"</font><font color='#000080'>/"&n&"</strong>頁</font> "
    response.write "<font color='#000080'> 共<b>"&totalnumber&"</b>篇文章 <b>"&maxperpage&"</b>篇文章/頁</font> "

    "直接輸入文章所在頁面轉到相關頁面
   response.write " <font color='#000080'>轉到:</font><input type='text' name='page' size=4 maxlength=10 class=smallInput value="&currentpage&">"
   response.write "<input class=buttonface type='submit'  value=' Goto '  name='cndok'></span></p></form>"     
end function
%>

   "文章搜尋相關程式
<form name="searchtitle" method="POST" action="index.asp">

   "把輸入的查詢字元賦值給txtitle,這樣在前面的顯示文章語句就起了作用title like '%"&request("txtitle")&"%'
      <p>標題:<input class="smallInput" type="text" name="txtitle" size="13"> <select class="smallSel" name="typeid" size="1">
<%

   "顯示與文章相關的欄目資訊,可以選擇一個進行查詢,這裡的typeid也賦值給了typeid,以使前面的顯示文章的程式對需要顯示內容做出判斷
  typesql="select * from type"
rstype.open typesql,conn,1,1
  do while not rstype.eof
                                sel="selected"    
             response.write "<option " & sel & " value='"+CStr(rstype("typeID"))+"' name=typeid>"+rstype("type")+"</option>"+chr(13)+chr(10)
             rstype.movenext
               loop
rstype.close
set conn=nothing
%>      </select><input class="buttonface" type="submit" value="查 詢" name="title"></p>
    </form>
    </div></td>
  </tr>
</table>
</center></div>
</body>
</html>

   在這裡順便介紹一下關於開啟文章的程式openarticle.asp,這個也是一個更新資料庫內容(update瀏覽數)和重新導向檔案。很簡單,所以這裡只是簡單介紹一下它的程式內容:
   
<%@ 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
%>

   好了,關於文章的顯示首頁面就完成了,它可以顯示文章的標題,加入日期等文章相關資訊,另外還結合了文章的分欄目搜尋查詢功能。在這裡你會發現,利用ASP對資料庫進行查詢並不是一件很難的事情,只要利用一個like就可以輕易實現,文章管理的前景程式我們已經基本完成,下面該來介紹管理系統的背景程式了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.