ASP进阶之文章在线管理更新(8)

ASP进阶之文章在线管理更新--主页面及搜索篇

作者:沙滩小子

经过了文章的添加、保存、显示,那么现在应该来谈谈关于管理程序的显示主页面,也就是显示所有文章的标题连接,以方便浏览者查找文章,其应该具有的功能有:显示所有文章的标题连接,加入日期,浏览次数等信息,另外还必须提供分页功能,要不然这么多的文章标题在一个页面都显示出来,那将非常的费时且不便浏览,另外由于本程序在这里结合了文章分栏目搜索的功能,所以在这里也将一起介绍了。

下面就为大家详细的介绍主页面index.asp的这些功能的实现过程以及其具体功用:

"建立数据库连接

 1<html>
 2<head>
 3<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
 4<title>ASP专题栏目</title>
 5<meta content="Microsoft FrontPage 3.0" name="GENERATOR"/>
 6<link href="style.css" rel="stylesheet" type="text/css"/>
 7<script language="javascript">   
 8function popwin2(path)   
 9{ window.open(path,"","height=450,width=600,resizable=yes,scrollbars=yes,status=no,toolbar=no,menubar=no,location=no");   
10}   
11</script>
12</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="&amp;cstr(typeid)
rstype.open typesql,conn,1,1
typename=rstype("type")
rstype.close

 1  
 2<body>
 3<div align="center"><center>
 4<table border="0" cellpadding="0" cellspacing="0" width="95%">
 5<tr>
 6<td style="border-left: thin dotted rgb(0,128,0); border-right: thin dotted rgb(0,128,0)" width="100%"><p align="center"><br/>   
 7  
 8"显示栏目信息,当你点击了任何一个栏目,在typename的位置都会显示相关信息,这里没有采用栏目的自动显示方式是考虑了页面的显示效果,而采用手工添加的模式,要想采用自动模式,就自己写吧:)相信你学习到现在,不会连这个都编不出来吧!   
 9动网ASP技巧专题&gt;&gt;<font color="#FF0000">```
10response.write ""&amp;amp;typename&amp;amp;""
11```</font><br/>
12<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>
13<div align="center">   
14  
15"打开指定的记录集article并按照文章的加入日期排序,在这里打开有两个条件,一个是利用like来查询数据库并显示相关文章标题,还有就是通过返回的typeid显示指定栏目的文章   

sql="select * from article where title like '%"&amp;request("txtitle")&amp;"%' 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&lt;1 then
currentpage=1
end if
"利用文章总数和每页最大文章数算得分页的页数
if (currentpage-1)*MaxPerPage&gt;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 "定义书签="" "显示文章标题及相关数据库内容子程序="" (currentpage-1)*maxperpage="" <div="" ```="" align="center" bookmark="rs.bookmark" currentpage="1" dim="" else="" end="" i="0" if="" rs="nothing" rs.close="" rs.move="" set="" showcontent="" showpage="" sub="" then="" totalput,maxperpage,"index.asp"="">

``` do while not rs.eof ```"依次显示文章ID号,文章标题,文章加入日期及浏览数,这里的openarticle.asp是用来重新定向文章的,以后将为大家介绍```

"这里是一个循环,每显示一篇文章,则定义的变量i的值加一,当i的值大于或等于页面最大文章数时退出循环
i=i+1
if i&gt;=MaxPerPage then exit do
"显示完一篇文章以后,自动移到记录集的下一个记录
rs.movenext
loop

 1</table>
 2</center></totalput></div>```
 3   
 4end sub   
 5  
 6"显示分页的function   
 7function showpage(totalnumber,maxperpage,filename)   
 8dim n   
 9  
10"利用文章数算出文章的分页数N   
11if totalnumber mod maxperpage=0 then   
12n= totalnumber \ maxperpage   
13else   
14n= totalnumber \ maxperpage+1   
15end if   
16response.write "&lt;form ?typeid="&amp;amp;typeid&amp;amp;" action="&amp;amp;filename&amp;amp;" method="Post"&gt;"   
17response.write "&lt;p align="center"&gt;&lt;font color="#000080"&gt;&amp;gt;&amp;gt;分页&lt;/font&gt; "   
18  
19"如果当前页数小于2,则显示的文章首页和上一页不显示连接,否则用当前页数减去1来显示上一页,直接用page=1来显示首页   
20if CurrentPage&amp;lt;2 then   
21response.write "&lt;font color="#000080"&gt;首页 上一页&lt;/font&gt; "   
22else   
23response.write "&lt;a ?page='1&amp;amp;typeid="&amp;amp;typeid&amp;amp;"' href="&amp;amp;filename&amp;amp;"&gt;首页&lt;/a&gt; "   
24response.write "&lt;a &amp;typeid="&amp;amp;typeid&amp;amp;" ?page="&amp;amp;CurrentPage-1&amp;amp;" href="&amp;amp;filename&amp;amp;"&gt;上一页&lt;/a&gt; "   
25end if   
26  
27"假如分页页数小于1,则直接显示下一页和尾页,否则用当前页数加上1来显示下一页,用已经算出的文章分页数N显示文章的尾页   
28if n-currentpage&amp;lt;1 then   
29response.write "&lt;font color="#000080"&gt;下一页 尾页&lt;/font&gt;"   
30else   
31response.write "&lt;a &amp;typeid="&amp;amp;typeid&amp;amp;" ?page="&amp;amp;(CurrentPage+1)&amp;amp;" href="&amp;amp;filename&amp;amp;"&gt;"   
32response.write "下一页&lt;/a&gt; &lt;a ?page="&amp;amp;n&amp;amp;" href="&amp;amp;filename&amp;amp;"&gt;尾页&lt;/a&gt;"   
33end if   
34  
35"用N和maxperpage显示文章的分页数和每页的文章数   
36response.write "&lt;font color="#000080"&gt; 页次:&lt;/font&gt;&lt;strong&gt;&lt;font color="red"&gt;"&amp;amp;CurrentPage&amp;amp;"&lt;/font&gt;&lt;font color="#000080"&gt;/"&amp;amp;n&amp;amp;"&lt;/font&gt;&lt;/strong&gt;页 "   
37response.write "&lt;font color="#000080"&gt; 共&lt;b&gt;"&amp;amp;totalnumber&amp;amp;"&lt;/b&gt;篇文章 &lt;b&gt;"&amp;amp;maxperpage&amp;amp;"&lt;/b&gt;篇文章/页&lt;/font&gt; "   
38  
39"直接输入文章所在页面转到相关页面   
40response.write " &lt;font color="#000080"&gt;转到:&lt;/font&gt;&lt;input class="smallInput" maxlength="10" name="page" size="4" type="text" value="¤tpage&amp;amp;"/&gt;"   
41response.write "&lt;input class="buttonface" name="cndok" type="submit" value=" Goto "/&gt;&lt;/p&gt;&lt;/form&gt;"   
42end function   

"文章搜索相关程序

"把输入的查询字符赋值给txtitle,这样在前面的显示文章语句就起了作用title like '%"&request("txtitle")&"%'

标题:

ID号文章标题加入日期点击

``` =rs("articleid") ```

``` =rs("title") ```

``` =rs("date") ```

``` =rs("hits") ```