ODBiC Discussion Board

RE: The Name1 is required in both loops, Gordon Hsu, 08-17-2006


by Roger Harris, August 17, 2006 16:54

The EACHMULTI loop was originally designed to handle input from a <SELECT MULTIPLE> HTML form field -- i.e. a drop-down list where you can use the Ctrl key to select multiple items from the list. The way that the HTTP CGI protocol handles that is to pass two or more name=value pairs with the exact same variable name (i.e. the NAME="..." specified in the SELECT tag). So, when ODBscript is setting up the variable table with the HTML form input, it does not check to see if a variable is already defined and replace the current value if so (which is what the SET statement does); it just adds each of the multiple name=value pairs to the variable table. When you design a form, you can also set up other types of input fields (such as text boxes) that all have the same NAME="..." variable name, and each of those inputs will be passed as a separate name=value pair, regardless of duplicates. If you pass values to a CGI in the URL, you can also use multiple "&name=value" pairs with the same name. In all of those cases, the CGI simple gets multiple name=value pairs with the same name, so ODBscript just puts them all in the variable table in the order they were received.

The SETMULTI statement was added later when I realized that if a specialized version of the SET statement also allowed multiple instances of the same variable name to be set, rather than replacing the current value, then you could essentially create an array in a script and then process it with the EACHMULTI loop, without needing the array subscript type of notation that most languages use.

Regardless of how the multiple instances of a variable were set, the EACHMULTI loop is designed to let you access those multiple instances one at a time. If the EACHMULTI statement is used with a single variable name (for example <% EACHMULTI Name1 %> ) then the code within the loop will reference the first value of that variable in the first pass, the second value in the second pass, and so on.

If the EACHMULTI statement is used with several variables, such as <% EACHMULTI Name1, Name2, Name3 %>, it really does the same thing: The first value found anywhere in the variable table for each of those variables is referenced in the first pass, the second value for each in the second pass, and so on.

But note that when you use a list of variables in an EACHROW loop, there isn't necessarily any real correlation between those sequential values for multiple variables unless your script code or the input form that set those values put them into the variable table in some sequentially correlated order. I think this is where you are confused: You have a SETMULTI inside an EACHROW loop that sets three variables, so if you used an EACHMULTI with those same three variables immediately after that, then you would get them back in the same order that they were set from the query result rows, so they would be correlated sets of values as you would expect. But then you have another EACHROW loop and use SETMULTI again, using one variable name that was also in the first loop plus two new variables. But that first Name1 variable that you are re-using still has values that were set in the first EACHROW loop, so the values from that second loop are added into the variable table following those existing values. So now, the "first instance" of Name1 is not correlated to the first instance of Name5 and Name6 because they were set from different database rows. I believe you are thinking that those first values are being discarded or ignored somehow, but they are not. The SETMULTI statement always just adds a new instance of a variable without checking to see whether or not the variable already has a value. You can give multiple variable=value pairs in the SETMULTI statement, but they are actually each handled separately, and you won't get back the same three values in the same EACHMULTI loop iteration unless all the variables *already* have exactly the same number of values set. If you think of it as being like simply adding values to columns in a two-dimensional table, but you're doing each column separately, then what's happened in your script is that your rows from the second EACHROW loop are not aligned the way you expected because the first column already had some entries.

The other thing that is happening is that, since there isn't any requirement that all the variables in the EACHMULTI statement have the same number of values, the EACHMULTI loop continues for as long as ANY of the specified variables has more values. If it runs out of values for any of the other variables, then it sets those to be empty for that pass. In your sample script, your Name1 variable will have more values than Name5 and Name6, so the loop does all of those Name1 values, setting Name5 and Name6 to be empty when they run out.

I hope this clarifies how the EACHMULTI works, but let me know if not. I'm not sure why you say you need to re-use that Name1 variable in the second loop, but using a different variable name would be the easiest way to fix the problem. If there's a reason why that won't work for your script, please explain what you need to do and I'll see if I can find another solution.


Post Your Reply:

E-mail  optional

HTTP Link: 
Link text: 


Copyright ©1997-2003, Roger Harris. All rights reserved.