<%
Sub AutoPage()
Dim MaxPageLen, page, content, PageNumber, ArticleID
Dim contentLen, BeginPoint, EndPoint, i
MaxPageLen = 1000 ' 每頁字元數
content = UBBCode(rs("content")) ' 假設有 UBB 轉 HTML 函數
ArticleID = rs("ArticleID")
contentLen = Len(content)
page = Request.QueryString("page")
If page = "" Then page = 1 Else page = CInt(page)
' 計算總頁數
PageNumber = Int(contentLen / MaxPageLen) + 1
If contentLen Mod MaxPageLen = 0 And contentLen > 0 Then PageNumber = PageNumber - 1
' 邊界修正
If page < 1 Then page = 1
If page > PageNumber Then page = PageNumber
' 計算擷取範圍
If page = 1 Then
BeginPoint = 1
Else
BeginPoint = MaxPageLen * (page - 1) + 1
End If
If page = PageNumber Then
EndPoint = contentLen
Else
EndPoint = MaxPageLen * page
If EndPoint > contentLen Then EndPoint = contentLen
End If
' 擷取並輸出當頁內容
content = Mid(content, BeginPoint, EndPoint - BeginPoint + 1)
Response.Write content & "
"
' 輸出頁碼導覽
For i = 1 To PageNumber
Response.Write "" & i & " "
Next
End Sub
%>
| 變數 | 說明 |
|---|---|
MaxPageLen | 每頁最大字元數,可依設計調整(如 2000、5000)。 |
BeginPoint/EndPoint | 使用 Mid 函數 1-based 索引擷取子字串。 |
PageNumber | 總頁數 = Int(總長度/每頁長度) + 1,修正整除情況。 |
直接對 HTML 字串切割會導致標籤不完整(如 `
MSHTML` (Microsoft HTML Object Library) 解析 DOM,遍歷節點累計文字長度,在安全邊界(文字節點、區塊元素結束)切分。HtmlAgilityPack(需 .NET 環境)。編輯時在適當位置插入 ``,輸出時以此為界分頁(見下一篇文章)。
<style>
.paged-content { column-width: 600px; column-gap: 2rem; }
/* 或使用 CSS Paged Media 模組列印分頁 */
</style>
適合列印導向,不適合一般網頁閱讀。
將分頁後的 HTML 片段快存至資料表或檔案,避免每次請求重複切割運算。
ALTER TABLE Articles ADD PageCache TEXT; -- 儲存 JSON 陣列 ["page1html","page2html",...]