首先感謝V37斑竹對我的幫助,這個方案解決了顯示“上一篇下一篇”和相關文章的問題, 貼出來讓大家分享。 以前看到一個帖子講用ID+1和ID-1的辦法判斷“上一篇下一篇”, 在用的過程中發現一個問題:當刪除數據庫中的一篇文章時,就會造成ID不連續, 如果用ID+1和ID-1來判斷就會出現找不到記錄的問題,在這個程序里, 通過查詢大于當前ID的第一條記錄來找出下一篇的ID, 查詢小于當前ID的第一條記錄來找出上一篇的ID,這樣就算ID不連續也可以正常顯示了。 至于相關文章的顯示則是在數據表里添加一個boardid字段來區分不同的文章欄目, 在每次添加一篇新文章時加上boardid號就可以了, 顯示一篇文章時根據boardid來查詢數據庫就能顯示出相關文章。 數據表articles中的字段有id,boardid,title,content,author,addtime。 <!--程序開始--> '定義一個thenext函數來找出下一篇的ID,如果當前記錄已經是最后一條記錄,則輸出文字“沒有了” <% 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函數來找出下一篇的ID,如果當前記錄已經是最前面的一條記錄,則輸出文字“沒有了” <% 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 %> '數據庫連接文件 <!--#include file="conn.asp"--> '取得傳遞過來的ID,顯示文章標題作者和內容 <% id=request("id") sql="select * from articles where id="&id set rs=conn.execute(sql) %> <% boardid=rs("boardid") %> <title>文章系統-<% =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"> <!--將數據庫的資料取出,經過編碼后輸出,保持輸入時的格式不變--> <%= replace(server.HTMLEncode(rs("content")),chr(13),"<br>") %></span></td></tr><% a1=rs("id") %><tr><td width="269" align="right"> <!--調用前面定義的顯示上一篇的函數--> <% thehead %> </td> <td width="257" align="right"> <!--調用前面定義的顯示下一篇的函數--> <% thenext %></td></tr> <% rs.MoveNext Loop %></table></td><td width="217" valign="top" align="left">相關文章: '根據當前文章的欄目號,找出同一欄目的文章 <%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> <!--程序結束-->
|