| 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
 |