The making of the effect of "the last article and the next chapter" in the article system

Source: Internet
Author: User
Tags define end sql
Used to refer to a site's source code, is the use of id+1 and ID-1 method to determine the context of the next. In the actual use of the process, found a problem: when the deletion of an article in the database, will cause the ID discontinuity, if you use id+1 and ID-1 to judge will appear to be unable to find records. So the program is not very perfect.
In the ASP code we are introducing, we find the next ID by querying the first record that is larger than the current ID, and the query is less than the first record of the current ID to find the ID of the previous one, so that even if the ID is discontinuous, it can be displayed correctly. (The program code is also referred to the classic forum related posts prompted by the production)
Simple Introduction Method:
The data table articles has the ID (article ID), title (article title), content (article content), Addtime (article publish time) and other related fields.
<!--database Connection file-->
<!--#include file= "conn.asp"-->
<%
' Define a nextrs function to find the ID of the next article
function Nextrs
Nextrsd=server. CreateObject ("Adodb.recordset")
Sql= ' SELECT top 1 * articles where id> ' &a1& ' ORDER by ID '
Set Nextrsd=conn.execute (SQL)
If Nextrsd.eof Then
Response. Write "Next: This is the last article under this category."
Else
A2=NEXTRSD ("id")
Response. Write "Next: <a href= ' article.asp?id=" &a2& "' >" &AMP;NEXTRSD ("title") & "</a>"
End If
End Function

' Define a BACKRS function to find the ID of the previous article
function Backrs
Backrsd=server. CreateObject ("Adodb.recordset")
Sql= "SELECT top 1 * articles where id<" &a1& "ORDER BY id DESC"
Set Backrsd=conn.execute (SQL)
If Backrsd.eof Then
Response. Write "Previous: This is the first article under this category"
Else
A0=BACKRSD ("id")
Response. Write "Previous: <a href= ' article.asp?id=" &a0& "' >" &AMP;BACKRSD ("title") & "</a>"
End If
End Function

' Get the article ID passed over
Id=request ("id")
Sql= "SELECT * from articles where id=" &id
Set Rs=conn.execute (SQL)
A1=RS ("id")
%>
<!--display article content ..............................-->
<!--call the function previously defined to display the previous article--><% Backrs%><br>
<!--call the function defined earlier to display the next article--><% Nextrs%>
<!--program End-->

Limited level, there may be some deficiencies in the code, such as the emergence of bugs, please give correct corrections. If you have any questions, please log in to Http://www.webjx.com/bbs for enquiries.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.