Click to See Complete Forum and Search --> : How do I loop through a select box to find first free spot?
Pelle
02-08-2005, 05:00 PM
I am experiencing some problems looping through a select box in my ASP code.
I retrieve a value from a form and want this value to be inserted in the first free spot in the select box. Unfortunately I have no idea how to write the code to loop through the different values, pass the existing values and quit looping when the first free spot is identified and then insert "formvalue" inserted.
' Value retrieved from form
formvalue = Request.form("name")
' Loop through select box (but how?) and search for first empty spot. ' When found assign "formvalue" to that spot. (that is right after ' "yet another value")
<select name="menu1">
<option>Some value </option>
<option>Some other value </option>
<option>Yet another value </option>
</select>
I hope some one would like to finish the code for me because it is driving me crazy...
lmf232s
02-08-2005, 07:42 PM
Were are the initial values for the select box coming from?
Are you reading them from a database?
Are they hardcoded?
Are you reading from a text file?
buntine
02-08-2005, 08:18 PM
You cant loop through hard-coded data. It doesnt really make sense. You can only really loop through the select element if you are generating it each time. The you could insert the extra value.
Otherwise, you just have to add it in explicitely.
<select name="menu1">
<option>Some value </option>
<option>Some other value </option>
<option>Yet another value </option>
<option><%= yourValue %></option>
</select>
Regards.
Pelle
02-09-2005, 12:20 PM
Sorry, I didn't understand that but I will try to explain my problem again... (this explanation contains more details and some more issues of my programming problem)
What I am trying to achieve is the following:
First I am going to check whether a session variable exists. If so, the values stored in session variable will be retrieved and stored in a local array. If not an array will be created. Then I want to loop through the array to find the first empty record and store the recently received value from the form ("formvalue"). When I have stored the new value in the local array all these values should be transfered/stored in the select box ("menu1") shown in the code. Lastly, I want to store the array in the session variable again, to be able to repeat this procedure as many times as I want. (Like a shopping cart :) )
Hope my explanation is enough for you to help me.
formvalue = Request.form("name")
'Check for existing session variable. If true, store that session variable in local array. If false create a local array. Assign form value to the first empty spot in the one applicable of these arrays.
Then put all array values in a select box.
Then store array values in session variable again.
lmf232s
02-09-2005, 02:02 PM
While this is not necessarly the solution to your problem,
It might help you understand. The hidsubmit and select case
have nothing to do with your situation, this is just how i
submit pages and decide what action to take. Its kind of like
calling a fucntion without calling a fucntion, anyway.
If you look in the case else statement, if you comment out the
session variable then youll see how it behaves when no session
variable it present to start. Now i put this together fast, so i
used brute force to get it to work, but it should give you an example
of how you can go about this and then come up with your own plan of
attack. It may not be the most efficient code but it gets the job
done, as well as giving an example.
Let me know if you have any problems.
<%option explicit%>
<%
'Our Array, Declared at 20, set to what ever you like, also use Redim if needed.
Dim NewArray(20)
Dim TempValue
Dim i
Dim bValid
'Pay not attention to the hidsubmit and select Case, this is how i submit pages and
'decide on what action to take.
Dim hidsubmit
hidsubmit = Request.Form("hidsubmit")
Select Case hidsubmit
Case "AddValue"
If Session("MyTest") = "" then 'we have no session variable, lets create 1
Session("MyTest") = Request.Form("txtValue")
'set the first position of the array with the value returned from the form.
NewArray(0) = Request.Form("txtValue")
else
'Session("MyTest") has a values, lets get them.
TempValue = Split(Session("MyTest"), ",")
'we split the session variable, lets see if its an array of values.
If IsArray(TempValue) then
'for each value of TempValue, set the value = to a position in the array.
For i = 0 to uBound(TempValue)
'load all the values from your Session Variable
NewArray(i) = TempValue(i)
Next
End if
'Now lets find the next empty spot to put your new value returned from the form.
For i = 0 to uBound(NewArray)
'once bValid = True then we no longer need to do this. Once bValid = True, that
'means we have alreay dropped into the loop and set the new value.
if bValid = False then
if NewArray(i) = "" then
'we have an empty position add new value
NewArray(i) = Request.Form("txtValue")
bValid = True
end if
end if
Next
'append our session variable to include the new value returned form the form.
Session("MyTest") = Session("MyTest") & "," & Request.Form("txtValue")
end if
Case Else
Session("MyTest") = "1,2,3,4,5,6,7"
If Session("MyTest") <> "" then
'Lets load our array, as we will need it to populate the select box below.
TempValue = Split(Session("MyTest"), ",")
If IsArray(TempValue) then
For i = 0 to uBound(TempValue)
NewArray(i) = TempValue(i)
Next
End if
end if
End Select
%>
<body>
<form name=form method=post>
<input type=hidden name=hidsubmit value="">
<select name=MySelect>
<%
'load the select box with the values of the array/Sessin variable.
for i = 0 to UBound(NewArray)%>
<option value="<%=NewArray(i)%>"><%=NewArray(i)%></option>
<%next%>
</select><br>
<input type=text name=txtValue><br>
<input type=button name=cmdSubmit value="Add Value" onclick="document.form.hidsubmit.value='AddValue';document.form.submit();">
</form>
</body>
Pelle
02-10-2005, 09:36 AM
Thanks a lot for the code you provided. After some modifications and adaptation of it, my code now works almost like I want (and a lot better than yesterday :) ).
There is one problem though. I also have a button that, when clicked, should empty the select box. I know how to accomplish that in JS but not in VB-S.
Should I use a function or procedure?
Anyone who would like to finish this code?
<%
' The "submit" button (maybe it's wrong to use a submit button?) is supposed to call the "delete" function/procedure to delete all array records.
%>
<input name="Submit" type="submit" id="Submit" value="Empty Array" onClick="delete()">
<%
' When this function is called all records in the array should be deleted. The array is then minimized to remove all empty records left. Last, the first record "0" is assigned the value "No record in array", which should be displayed in the select box.
Function delete
For i = 0 to uBound(MyArray)
MyArray(i) = ""
Next
ReDim MyArray(0)
MyArray(0) = "No record in array"
End function
%>
<select name=MySelect>
<%
'Load the select box with the value of the array
For i = 0 to uBound(MyArray)%>
<option value="<%=MyArray(i)%>"><%=MyArray(i)%></option>
<%Next%>
</select>