依據慣例命名:資料表 city_前綴,主鍵 前綴_id,標題 前綴_title,顯示頁 show_前綴.asp,即可寫出通用函數。
Function GetPre(pid, prame, ptable)
Dim id, title, table, url, sql, rs, pre
id = prame & "_id"
title = prame & "_title"
table = "city_" & ptable
url = "show_" & ptable
sql = "SELECT TOP 1 " & id & "," & title & " FROM " & table & " WHERE " & id & " < " & pid & " ORDER BY " & id & " DESC"
Set rs = Conn.Execute(sql)
If rs.EOF Or rs.BOF Then
pre = "上一篇:沒有新聞了"
Else
pre = "<a href=" & url & ".asp?" & id & "=" & rs(0) & ">" & rs(1) & "</a>"
End If
GetPre = pre
End Function
Function GetNext(nid, nrame, ntable)
Dim id, title, table, url, sql, rs, nnext
id = nrame & "_id"
title = nrame & "_title"
table = "city_" & ntable
url = "show_" & ntable
sql = "SELECT TOP 1 " & id & "," & title & " FROM " & table & " WHERE " & id & " > " & nid & " ORDER BY " & id
Set rs = Conn.Execute(sql)
If rs.EOF Or rs.BOF Then
nnext = "下一篇:沒有新聞了"
Else
nnext = "<a href=" & url & ".asp?" & id & "=" & rs(0) & ">下一篇:" & rs(1) & "</a>"
End If
GetNext = nnext
End Function
資料表 city_note,欄位 cn_id、cn_title,顯示頁 show_note.asp:
<%=GetPre(cn_id, "cn", "note")%> ' 上一篇
<%=GetNext(cn_id, "cn", "note")%> ' 下一篇
ORDER BY 欄位改為日期欄位即可。