Arrays 如何在经典asp中使用索引获取数组字符串值

Arrays 如何在经典asp中使用索引获取数组字符串值,arrays,vbscript,asp-classic,Arrays,Vbscript,Asp Classic,我需要使用索引位置值从存储在数组中的值列表中检索一个特定值 这是来自url参数的位置引用: Dim currentorder Dim nextorderindex Dim rowindex Dim iArray Dim i currentorder = Trim(Request.QueryString("order")) rowindex = CINT(Trim(Request.QueryString("rowindex"))) 'e.g. let's say this is a

我需要使用索引位置值从存储在数组中的值列表中检索一个特定值

这是来自url参数的位置引用:

Dim currentorder     
Dim nextorderindex
Dim rowindex 
Dim iArray
Dim i

currentorder = Trim(Request.QueryString("order"))
rowindex = CINT(Trim(Request.QueryString("rowindex")))  'e.g. let's say this is a 4

SQLOrderList = "SELECT orderno" & _
                " FROM awd_order" & _
                " WHERE order_date >= '" & dtstart & " 00:00:00'" & _
                " AND order_date < '" & dtend & " 23:59:59'" & _                    
                " ORDER BY " & sortby

objRS.Open SQLOrderList, objConn

i = objRS.RecordCount  'e.g. this contains 7 records            

If Not rowindex >= (i - 1) Then     'e.g. 4 >= (7 - 1)
    nextorderindex = (rowindex + 1) 'e.g. then nextorderindex = 5
End If

'Get recordset to Array
iArray = objRS.GetRows()   

If nextorderindex > 0 Then    'e.g. nextorderindex is 5
    response.write iArray(nextorderindex)  'e.g. here is my issue, I get: subscript out of range error
Else
    response.write currentorder
End If
Dim currentorder
Dim nextorderindex
暗行索引
昏暗的光线
昏暗的我
currentorder=Trim(Request.QueryString(“订单”))
rowindex=CINT(Trim(Request.QueryString(“rowindex”))”例如,假设这是一个4
SQLOrderList=“选择订单号”&_
“来自awd_订单”和_
“WHERE order_date>=”&dtstart&“00:00:00'”&_
“和订单日期<'”&dtend&“23:59:59”&”
“订购人”和sortby
打开SQLOrderList,objConn
i=objRS.RecordCount'例如,它包含7条记录
如果不是行索引>=(i-1),则“例如4>=(7-1)
nextorderindex=(行索引+1)”例如,然后nextorderindex=5
如果结束
'将记录集获取到数组
iArray=objRS.GetRows()
如果nextorderindex>0,则“例如nextorderindex为5
response.write iArray(nextorderindex)”例如,这是我的问题,我得到:下标超出范围错误
其他的
response.write currentorder
如果结束
GetRows返回二维数组有关详细信息,请阅读以下内容:


我没有运行这段代码,但希望这将工作,如果没有复制错误,我会更新代码。希望这会有所帮助

您的代码非常混乱,您没有定义currentorder变量并在if条件下使用它,如果next nextorderindex大于0,则为其分配5,如果返回的记录小于5,则始终生成超出范围的索引。我添加了currentorder声明,这是查询字符串中的一个参数。我为代码混乱道歉,我很难理解如何在数组中移动以获得下一个可用订单。如果url中的rowindex值为4,则数组中的当前顺序具有索引4,如果希望使用索引值获取下一个顺序,则必须增加索引4+1,并从与该索引匹配的数组中获取字符串值。这有意义吗?我正在重做您的代码,请稍候,如果它返回的记录少于5条,那么就没有下一个可用的订单,我应该留下/打印当前订单,只是为了进一步改进这一点,根据saqibahmad所说的-最好在代码开头包含
Option Explicit
。这将强制执行变量声明,并防止您以后可能遇到的问题。嗨,saqibahmad,谢谢您的帮助。我运行了它,它比我现有的运行得更好,但是我注意到索引值跳过了数组中的一个。如果我有一个数组包含:A1G722(1)、A1G723(2)、A1G724(3)、A1G725(4)、A1G726(4)、A1G727(4)、A1G728(4),然后我传递“A1G724(3),代码将返回A1G726(4)。这是因为数组是基于零的吗?非常感谢。我标记了。为了解决这个问题,是否只需从URL中传递的行索引值中减去1(该值不是从零开始的)?例如,rowindex=(CINT(Trim(Request.QueryString(“rowindex”))-1)您可以在以下步骤中执行:如果rowindex<(i),则nextorderindex=(rowindex)不增加它。@saqibahmad:
选项显式
-非常有用!
Dim currentorder     
Dim nextorderindex
Dim rowindex 
Dim iArray
Dim i

currentorder = Trim(Request.QueryString("order")) // e.g. 4
rowindex = CINT(Trim(Request.QueryString("rowindex")))  '' lets say this is a 4

SQLOrderList = "SELECT orderno" & _
            " FROM _order" & _
            " WHERE CAST(FLOOR(CAST(order_date AS FLOAT)) AS DATETIME)" & _
            " BETWEEN CAST('" & dtstart & "' AS DATETIME)" & _
            " AND CAST('" & dtend & "' AS DATETIME)" & _     
            " ORDER BY " & sortby    
objRS.Open SQLOrderList, objConn

i = objRS.RecordCount  ' this contains 7 records
if i > 0 then
If Not isNumeric(rowindex) Or rowindex = "" Then 
    rowindex = 0 
End If 
If rowindex < (i) Then  ' 4 >= (7 - 1)
    nextorderindex = (rowindex + 1) ' then nextorderindex = 5
else
    nextorderindex = 0
End If
If nextorderindex > 0 Then 'nextorderindex is 5
    response.write iArray(0,nextorderindex) ' **here is my problem, I get     subscript out of range error**
Else
    response.write currentorder
End If
end if