ASP - Changing variable name (numbering) or adding numbers to a variable in asc order to assign values

主宰稳场 提交于 2019-12-25 19:05:25

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/36599409/asp-changing-variable-name-numbering-or-adding-numbers-to-a-variable-in-asc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!