如何在经典ASP中创建的表中为结果添加分页?

时间:2019-01-30 21:52:44

标签: vbscript asp-classic

我在VBScript中完成了一些创建表的代码。具体来说,代码从数据库中提取信息,然后循环遍历结果,将其添加到表中。问题是此表中有14,000行。每次尝试加载此页面时,我都会收到500内部服务器错误,我认为这是由于内存不足所致。

对于循环,我有这个:

<%
fHideNavBar = False
fHideNumber = False
fHideRequery = False
fHideRule = False
stQueryString = ""
fEmptyRecordset = False
fFirstPass = True
fNeedRecordset = False
fNoRecordset = False
tBarAlignment = "Left"
tHeaderName = "DataRangeHdr1"
tPageSize = 0
tPagingMove = ""
tRangeType = "Text"
tRecordsProcessed = 0
tPrevAbsolutePage = 0
intCurPos = 0
intNewPos = 0
fSupportsBookmarks = True
fMoveAbsolute = False

If IsEmpty(Session("DataRangeHdr1_Recordset")) Then
    fNeedRecordset = True
Else
    If Session("DataRangeHdr1_Recordset") Is Nothing Then
        fNeedRecordset = True
    Else
        Set DataRangeHdr1 = Session("DataRangeHdr1_Recordset")
    End If
End If

If fNeedRecordset Then
    Set DataConn = Server.CreateObject("ADODB.Connection")
    DataConn.Open "DSN=MYDSN","MyUserName","MyPassword"
    Set cmdTemp = Server.CreateObject("ADODB.Command")
    Set DataRangeHdr1 = Server.CreateObject("ADODB.Recordset")
    cmdTemp.CommandText = "SELECT PHONE, FAX, FIRM, ID FROM NNYBEA ORDER BY ID"
    cmdTemp.CommandType = 1

    Set cmdTemp.ActiveConnection = DataConn
    DataRangeHdr1.Open cmdTemp, , 0, 1
End If
On Error Resume Next
If DataRangeHdr1.BOF And DataRangeHdr1.EOF Then fEmptyRecordset = True
On Error Goto 0
If Err Then fEmptyRecordset = True

If Not IsEmpty(Session("DataRangeHdr1_Filter")) And Not fEmptyRecordset Then
    DataRangeHdr1.Filter = Session("DataRangeHdr1_Filter")
    If DataRangeHdr1.BOF And DataRangeHdr1.EOF Then fEmptyRecordset = True
End If

If fEmptyRecordset Then
    fHideNavBar = True
    fHideRule = True
End If
Do
    If fEmptyRecordset Then Exit Do
    If Not fFirstPass Then
        DataRangeHdr1.MoveNext
    Else
        fFirstPass = False
    End If
    If DataRangeHdr1.EOF Then Exit Do
%>
<tr>
<td><p align="center"><%= DataRangeHdr1("FIRM") %></td>
<td><p align="center"><%= DataRangeHdr1("PHONE") %></td>
<td><p align="center"><%= DataRangeHdr1("FAX") %></td>
<%end if%>
</tr>


<%
Loop%>

现在,我相信我之前的程序员基本上是从以下网站复制代码的:http://www.nnybe.com/board%20members/DEFAULT.ASP

实际上,我实际上更改了循环中的列名以匹配网站,因为它是如此相似(我的真实列名是不同的)。循环后,我的代码如下:

</TABLE>
<%
If tRangeType = "Table" Then Response.Write "</TABLE>"
If tPageSize > 0 Then
    If Not fHideRule Then Response.Write "<HR>"
    If Not fHideNavBar Then
        %>
        <TABLE WIDTH=100% >
        <TR>
            <TD WIDTH=100% >
                <P ALIGN=<%= tBarAlignment %> >
                <FORM <%= "ACTION=""" & Request.ServerVariables("PATH_INFO") & stQueryString & """" %> METHOD="POST">
                    <INPUT TYPE="Submit" NAME="<%= tHeaderName & "_PagingMove" %>" VALUE="   &lt;&lt;   ">
                    <INPUT TYPE="Submit" NAME="<%= tHeaderName & "_PagingMove" %>" VALUE="   &lt;    ">
                    <INPUT TYPE="Submit" NAME="<%= tHeaderName & "_PagingMove" %>" VALUE="    &gt;   ">
                    <% If fSupportsBookmarks Then %>
                        <INPUT TYPE="Submit" NAME="<%= tHeaderName & "_PagingMove" %>" VALUE="   &gt;&gt;   ">
                    <% End If %>
                    <% If Not fHideRequery Then %>
                        <INPUT TYPE="Submit" NAME="<% =tHeaderName & "_PagingMove" %>" VALUE=" Requery ">
                    <% End If %>
                </FORM>
                </P>
            </TD>
            <TD VALIGN=MIDDLE ALIGN=RIGHT>
                <FONT SIZE=2>
                <%
                If Not fHideNumber Then
                    If tPageSize > 1 Then
                        Response.Write "<NOBR>Page: " & Session(tHeaderName & "_AbsolutePage") & "</NOBR>"
                    Else
                        Response.Write "<NOBR>Record: " & Session(tHeaderName & "_AbsolutePage") & "</NOBR>"
                    End If
                End If
                %>
                </FONT>
            </TD>
        </TR>
        </TABLE>
    <%
    End If
End If
%>
</TABLE>

我从PagingMove部分的<和>中猜测,这应该允许分页。但是,我什至没有在我的页面上看到它。我不知道上面链接上的代码是否在他们的网站上有效,但是对于我自己的网站,我会问:

如何修改此代码以提供单击数据结果页面的选项,以便服务器不会耗尽内存?

如果对此有一个更优雅的解决方案可以完成相同的事情,我也将不胜感激!

1 个答案:

答案 0 :(得分:1)

在您的SQL中,您可以添加LIMIT偏移量

SELECT PHONE, FAX, FIRM, ID FROM NNYBEA ORDER BY ID LIMIT 0,10 ' Results 1 to 10
SELECT PHONE, FAX, FIRM, ID FROM NNYBEA ORDER BY ID LIMIT 10,10 ' 11 - 20
SELECT PHONE, FAX, FIRM, ID FROM NNYBEA ORDER BY ID LIMIT 20,10 ' 21 - 30
...

如果您使用的是MySQL,则可以使用...

SELECT SQL_CALC_FOUND_ROWS PHONE, FAX, FIRM, ID FROM NNYBEA ORDER BY ID LIMIT 0,10

...以获取结果的总数并计算要显示的页面链接数:

(total_results/results_per_page) ' and round up.

然后链接到结果表下方的页面,并将页码作为查询字符串传递:

default.asp?page=1
default.asp?page=2
default.asp?page=3
...

页面顶部有一些代码,可以获取请求的页码并计算正确的偏移值:

<%

Const results_per_page = 10

Dim limit_offset, page_num

limit_offset = 0 ' default
page_num = request.querystring("page")

if isNumeric(page_num) then
    page_num = int(page_num)
    if page_num > 0 then
        limit_offset = (page_num-1)*results_per_page
    else
        page_num = 1 ' default   
    end if
else
    page_num = 1 ' default    
end if

%>

最后,将限制偏移量应用于您的SQL:

cmdTemp.CommandText = "SELECT PHONE, FAX, FIRM, ID FROM NNYBEA ORDER BY ID LIMIT " & limit_offset & "," & results_per_page

您还可以使用GetRows()将记录集转换为2D数组并在循环时应用限制

Dim r, rs_loops, theData

theData = DataRangeHdr1.getRows()

rs_loops = page_num*results_per_page 

if rs_loops > uBound(theData,2) then rs_loops = uBound(theData,2)

for r = limit_offset to rs_loops

' output data from the DataRangeHdr1 recordset

%>

<tr>
<td><p align="center"><%= theData(2,r) ' firm %></td>
<td><p align="center"><%= theData(0,r) ' phone %></td>
<td><p align="center"><%= theData(1,r) ' fax %></td>
</tr>

<%

next

但这将意味着在内存中存储大量看不见的数据。在SQL中使用LIMIT偏移量会更有意义。