萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> ASP 程式設計

ASP 實作上一篇/下一篇文章導覽

通用函數設計

依據慣例命名:資料表 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 欄位改為日期欄位即可。
copyright © 萬盛學電腦網 all rights reserved