The core SQL statements of the previous article and the next article are as follows:
Previous
| The code is as follows: |
Copy code |
| Select top 1 Id from [table] where Id <@ current ID order by Id desc |
Next Article
| The code is as follows: |
Copy code |
| Select top 1 Id from [table] where Id> @ current ID order by Id |
In the future, no matter how asp php jsp asp.net is implemented, we will introduce asp and php practices.
Add the following code to the news. asp file:
| The code is as follows: |
Copy code |
|
<%
Set backrsd = server. CreateObject ("Adodb. recordset ")
SQL = "select top 1 * from news where id <" & id & "order by id desc"
Set backrsd=conn.exe cute (SQL)
If backrsd. eof then
NewTitle4 = "already the first article"
Else
Id2 = backrsd ("id ")
NewTitle2 = backrsd ("title ")
End If
Backrsd. close
Set nextrsd = server. CreateObject ("adodb. recordset ")
SQL = "select top 1 * from news where id>" & id & "order by id"
Set nextrsd=conn.exe cute (SQL)
If nextrsd. eof then
NewTitle3 = "this is the last article"
Else
Id1 = nextrsd ("id ")
NewTitle1 = nextrsd ("title ")
End If
Nextrsd. close
%>
|
Add the following code to <body>:
| The code is as follows: |
Copy code |
<Br> previous article: <% = NewTitle4 %> <a href = 'news. asp? Newid = <% = id2 %> '> <% = NewTitle2 %> </a>
<Br> next article: <% = NewTitle3 %> <a href = 'news. asp? Newid = <% = id1 %> '> <% = NewTitle1 %> </a> |
Php Usage
| The code is as follows: |
Copy code |
|
Function nextpage ($ fid, $ tag, $ bid ){
If ('next' = $ tag ){
$ SQL = "select * from gx_news where id <$ fid and menu_id = '$ bid' order by id desc ";
} Elseif ('prev' ==$ tag ){
$ SQL = "select * from gx_news where id> $ fid and menu_id = '$ bid' order by id asc ";
}
$ Result = mysql_query ($ SQL );
If (! Mysql_num_rows ($ result )){
Echo ('No more! ');
} Else {
$ Rs = mysql_fetch_array ($ result );
Echo "<a href = news_page.php? Id = ". $ rs [0]."> ". $ rs ['new _ title']." </a> ";
}
} |
Call
| The code is as follows: |
Copy code |
Nextpage ($ fid, 'next', $ bid); next page
Nextpage ($ fid, 'prev', $ bid); Previous Page |
Well, I am talking about only two ways to implement the previous and next implementations of all programming languages.