ASP文章系統(tǒng)解決方案實現(xiàn)上一頁下一頁第1/2頁
更新時間:2007年01月03日 00:00:00 作者:
首先感謝V37斑竹對我的幫助,這個方案解決了顯示“上一篇下一篇”和相關(guān)文章的問題,貼出來讓大家分享。
以前看到一個帖子講用ID+1和ID-1的辦法判斷“上一篇下一篇”,在用的過程中發(fā)現(xiàn)一個問題:當刪除數(shù)據(jù)庫中的一篇文章時,就會造成ID不連續(xù),如果用ID+1和ID-1來判斷就會出現(xiàn)找不到記錄的問題,在這個程序里,通過查詢大于當前ID的第一條記錄來找出下一篇的ID,查詢小于當前ID的第一條記錄來找出上一篇的ID,這樣就算ID不連續(xù)也可以正常顯示了。
至于相關(guān)文章的顯示則是在數(shù)據(jù)表里添加一個boardid字段來區(qū)分不同的文章欄目,在每次添加一篇新文章時加上boardid號就可以了,顯示一篇文章時根據(jù)boardid來查詢數(shù)據(jù)庫就能顯示出相關(guān)文章。
數(shù)據(jù)表articles中的字段有id,boardid,title,content,author,addtime
<!--程序開始-->
'定義一個thenext函數(shù)來找出下一篇的ID,如果當前記錄已經(jīng)是最后一條記錄,則輸出文字“沒有了”
<%
function thenext
newrs=server.CreateObject("adodb.recordset")
sql="select top 1 * from articles where id>"&a1&" order by id"
set newrs=conn.execute(sql)
if newrs.eof then
response.Write("沒有了")
else
a2=newrs("id")
response.Write("<a href='view.asp?id="&a2&"'>下一篇</a>")
end if
end function
%>
'定義一個thehead函數(shù)來找出下一篇的ID,如果當前記錄已經(jīng)是最前面的一條記錄,則輸出文字“沒有了”
<%
function thehead
headrs=server.CreateObject("adodb.recordset")
sql="select top 1 * from articles where id<"&a1&" order by id desc"
set headrs=conn.execute(sql)
if headrs.eof then
response.Write("沒有了")
else
a0=headrs("id")
response.Write("<a href='view.asp?id="&a0&"'>上一篇</a>")
end if
end function
%>
'數(shù)據(jù)庫連接文件
<!--#include file="conn.asp"-->
'取得傳遞過來的ID,顯示文章標題作者和內(nèi)容
<%
id=request("id")
sql="select * from articles where id="&id
set rs=conn.execute(sql)
%>
<% boardid=rs("boardid") %>
<title>文章系統(tǒng)-<% =rs("title") %></title><body leftmargin="0" topmargin="0">
<!--#include file="top.asp" -->
<%
Do While Not rs.EOF
%>
<table width="773" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td width="576" align="left">
<table width="557" border="0" cellspacing="5" cellpadding="4" align="left">
<tr>
<td colspan="2" align="center"><span style="font-size:9pt color:#efefef"><%= rs("title") %><br>
<div align="right"><span style="font-size:9pt color:#efefef">作者:<%= rs("author") %></span></div>
</span></td>
</tr>
<tr>
<td colspan="2" ><span style="font-size:9pt color:#efefef"><!--將數(shù)據(jù)庫的資料取出,經(jīng)過編碼后輸出,保持輸入時的格式不變--><%= replace(server.HTMLEncode(rs("content")),chr(13),"<br>") %></span></td>
</tr>
<% a1=rs("id") %>
<tr>
<td width="269" align="right"><!--調(diào)用前面定義的顯示上一篇的函數(shù)--><% thehead %></td>
<td width="257" align="right"><!--調(diào)用前面定義的顯示下一篇的函數(shù)--><% thenext %></td>
</tr>
<% rs.MoveNext%>
<%Loop%>
</table></td>
<td width="217" valign="top" align="left">相關(guān)文章:
'根據(jù)當前文章的欄目號,找出同一欄目的文章
<%
sql="select * from articles where boardid="&boardid&""
set rs=conn.execute(sql)
%>
<%
Do While Not rs.EOF
%>
<table width="207" border="0" cellspacing="2" cellpadding="2">
<tr>
<td height="20"><a href="view.asp?id=<%=rs("id")%>"><%= rs("title") %></a></td>
</tr>
</table>
<% rs.MoveNext%>
<%Loop%>
</td>
</tr>
</table>
<!--#include file="copyright.asp" -->
</body>
<!--程序結(jié)束-->
以前看到一個帖子講用ID+1和ID-1的辦法判斷“上一篇下一篇”,在用的過程中發(fā)現(xiàn)一個問題:當刪除數(shù)據(jù)庫中的一篇文章時,就會造成ID不連續(xù),如果用ID+1和ID-1來判斷就會出現(xiàn)找不到記錄的問題,在這個程序里,通過查詢大于當前ID的第一條記錄來找出下一篇的ID,查詢小于當前ID的第一條記錄來找出上一篇的ID,這樣就算ID不連續(xù)也可以正常顯示了。
至于相關(guān)文章的顯示則是在數(shù)據(jù)表里添加一個boardid字段來區(qū)分不同的文章欄目,在每次添加一篇新文章時加上boardid號就可以了,顯示一篇文章時根據(jù)boardid來查詢數(shù)據(jù)庫就能顯示出相關(guān)文章。
數(shù)據(jù)表articles中的字段有id,boardid,title,content,author,addtime
復制代碼 代碼如下:
<!--程序開始-->
'定義一個thenext函數(shù)來找出下一篇的ID,如果當前記錄已經(jīng)是最后一條記錄,則輸出文字“沒有了”
<%
function thenext
newrs=server.CreateObject("adodb.recordset")
sql="select top 1 * from articles where id>"&a1&" order by id"
set newrs=conn.execute(sql)
if newrs.eof then
response.Write("沒有了")
else
a2=newrs("id")
response.Write("<a href='view.asp?id="&a2&"'>下一篇</a>")
end if
end function
%>
'定義一個thehead函數(shù)來找出下一篇的ID,如果當前記錄已經(jīng)是最前面的一條記錄,則輸出文字“沒有了”
<%
function thehead
headrs=server.CreateObject("adodb.recordset")
sql="select top 1 * from articles where id<"&a1&" order by id desc"
set headrs=conn.execute(sql)
if headrs.eof then
response.Write("沒有了")
else
a0=headrs("id")
response.Write("<a href='view.asp?id="&a0&"'>上一篇</a>")
end if
end function
%>
'數(shù)據(jù)庫連接文件
<!--#include file="conn.asp"-->
'取得傳遞過來的ID,顯示文章標題作者和內(nèi)容
<%
id=request("id")
sql="select * from articles where id="&id
set rs=conn.execute(sql)
%>
<% boardid=rs("boardid") %>
<title>文章系統(tǒng)-<% =rs("title") %></title><body leftmargin="0" topmargin="0">
<!--#include file="top.asp" -->
<%
Do While Not rs.EOF
%>
<table width="773" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td width="576" align="left">
<table width="557" border="0" cellspacing="5" cellpadding="4" align="left">
<tr>
<td colspan="2" align="center"><span style="font-size:9pt color:#efefef"><%= rs("title") %><br>
<div align="right"><span style="font-size:9pt color:#efefef">作者:<%= rs("author") %></span></div>
</span></td>
</tr>
<tr>
<td colspan="2" ><span style="font-size:9pt color:#efefef"><!--將數(shù)據(jù)庫的資料取出,經(jīng)過編碼后輸出,保持輸入時的格式不變--><%= replace(server.HTMLEncode(rs("content")),chr(13),"<br>") %></span></td>
</tr>
<% a1=rs("id") %>
<tr>
<td width="269" align="right"><!--調(diào)用前面定義的顯示上一篇的函數(shù)--><% thehead %></td>
<td width="257" align="right"><!--調(diào)用前面定義的顯示下一篇的函數(shù)--><% thenext %></td>
</tr>
<% rs.MoveNext%>
<%Loop%>
</table></td>
<td width="217" valign="top" align="left">相關(guān)文章:
'根據(jù)當前文章的欄目號,找出同一欄目的文章
<%
sql="select * from articles where boardid="&boardid&""
set rs=conn.execute(sql)
%>
<%
Do While Not rs.EOF
%>
<table width="207" border="0" cellspacing="2" cellpadding="2">
<tr>
<td height="20"><a href="view.asp?id=<%=rs("id")%>"><%= rs("title") %></a></td>
</tr>
</table>
<% rs.MoveNext%>
<%Loop%>
</td>
</tr>
</table>
<!--#include file="copyright.asp" -->
</body>
<!--程序結(jié)束-->
相關(guān)文章
如何在數(shù)據(jù)庫中用好Transaction?
如何在數(shù)據(jù)庫中用好Transaction?...2006-06-06asp快速開發(fā)方法之數(shù)據(jù)操作實例代碼
asp快速開發(fā)方法之數(shù)據(jù)操作實例代碼...2007-08-08