ASP進階之文章在線管理更新--主頁面及搜索篇
作者:沙灘小子
經過了文章的添加、保存、顯示,那么現在應該來談談關于管理程序的顯示主頁面,也就是顯示所有文章的標題連接,以方便瀏覽者查找文章,其應該具有的功能有:顯示所有文章的標題連接,加入日期,瀏覽次數等信息,另外還必須提供分頁功能,要不然這么多的文章標題在一個頁面都顯示出來,那將非常的費時且不便瀏覽,另外由于本程序在這里結合了文章分欄目搜索的功能,所以在這里也將一起介紹了。
下面就為大家詳細的介紹主頁面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="¤tpage&">"
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就可以輕易實現,文章管理的前臺程序我們已經基本完成,下面該來介紹管理系統的后臺程序了。下一節我們將從管理員的密碼及密碼驗證開始介紹管理系統的后臺程序!講了這么多你是否有什么疑問呢,要是有什么問題可以發EMAIL給我:wodeail@etang.com,本程序的具體實例在:http://asky.on.net.cn/article
轉載請注明出處http://asky.on.net.cn