ASP - Changing variable name (numbering) or adding

2019-09-24 00:51发布

How to change the variable name numbering in ascending order to assign values to them. eg: car_1, car_2, car_3, car_4........ so on.. my coding is something like;

for i=1 to 20
var(i) = request.form("car_"i)
next

foreach ......so on........

response.write(var(12) & "<br/>")

I need a way to increase the number of 'car_' to assign each car value to the 'var' array. I have tried to add it like this:

var(i) = request.form("car_"&i)

AND

var(i) = request.form("car_"i"")

and none of these work. I would very much appreciate your help to solve this.

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-09-24 01:41

The example isn't very clear ideally it could be better but the more I look at it the more I think you are using VBScript, so I'm going to try an interpret what you are trying to do.

Dim i
Dim min_i: min_i = 1
Dim max_i: max_i = 20
Dim vars(max_i)

For i = min_i To max_i
  vars(i) = Request.Form("car_" & i)
Next
'Returns the value of Request.Form("car_12")
Call Response.Write(vars(12) & "<br />")

The approach was sound you just needed to concatenate (&) the value of i on to the name of the Request.Forms value.


It's worth pointing out that this is no different to what @David suggests in their answer except that this example tries to stay as close to the original requirement as possible by outputting the values to an Array instead of directly to the response buffer.

查看更多
对你真心纯属浪费
3楼-- · 2019-09-24 01:42

You can concatenate values in VBScript with the & operator. Such as:

"car_" & i

To demonstrate, go ahead and run this code in something like this online code editor (IE only, I suspect):

<html>
    <head>
        <script type="text/vbscript">
            For i = 1 To 20
                document.write "car_" & i
                document.write "<br />"
            Next
        </script>
    </head>
    <body>
    </body>
</html>

Which produces the following output:

car_1
car_2
car_3
car_4
car_5
car_6
car_7
car_8
car_9
car_10
car_11
car_12
car_13
car_14
car_15
car_16
car_17
car_18
car_19
car_20

The same also works in server-side VBScript:

<body>
    <% For i = 1 To 20 %>
        Car_<%=i%><br />
    <% Next %>
</body>

Which produces the same output.

查看更多
登录 后发表回答