473,500 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Loop to create an array from a dynamic form.

Loop to create an array from a dynamic form.

I'm having trouble with an application, and I'll try to explain it as
clearly as possible:

1. I have a form with two fields, say Apples and Oranges. The user
selects from a drop down menu, between 1-10 of each and clicks submit.
The resulting page will display a NEW form, with rows and a list of
fields for the amount of each items selected.

Example. If I selected 3 apples and 5 oranges, the resulting page
would show 3 rows of text fields for apples and 5 rows of text fields
for oranges.

The form fields would be (after each semicolon, the first word is the
form element name (Apple1, Apple2)):

Apple1: net price | gross price | date | time
Apple2: net price | gross price | date | time
Apple3: net price | gross price | date | time
Orange1: net price | gross price | date | time
Orange2: net price | gross price | date | time
Orange3: net price | gross price | date | time
Orange4: net price | gross price | date | time
Orange5: net price | gross price | date | time
I want to insert each one of these records into the database, each
record being a net, gross price, date and time for apple 1-X and
orange 1-X. The apples will go into the apple table and the oranges
will go into the oranges table.
I'm used to looping like this:

for each item in request.form
if left(item, 6) = "orange" then
orange = orange & request.form(item) & ","
end if
next
After that, I'll create a loop that inserts each record into the
database separately, something of this nature:
for each item in orange
set rsOrange= server.createobject("adodb.recordset")
strSql = "select * from oranges"
rsOrange.open strSql, Connstr, 1, 3

rsOrange.Addnew
rsOrange("net") = request.form("net")
rsOrange("gross") = request.form("gross")
rsOrange("date") = request.form("date")
rsOrange("time") = request.form("time")
rsOrange.Update
rsOrange.close
next
But since this is a multi dimensional array (gross, net, date, time
will need to be inserted correspondingly), you can see my dilemma. I
understand how they work when hard coded, but not to dynamically
create and populate them, then use in an insert statement. Sorry if
this is elementary, but any help would really be appreciated.

Thanks!
Jul 19 '05 #1
2 4063
You have to number your net, gross, date, and time form fields as well.
(Why do you have separate date and time columns in your table? Why not just
one field for this data?)

Also, when you're doing INSERTs, don't create a recordset object.

Try this code. (Three files)
FORM.HTM:
<html><head></head>
<body>
<form method="post" action="form.asp">
Apples: <input name="apples" type="text" />
Oranges: <input name="oranges" type="text" />
<input type="submit" />
</form>
</body>
</html>

--------------------------------------
FORM.ASP:
<html><head></head>
<body>
<%
Dim iOranges, iApples
iApples = Request.Form("apples")
iOranges = Request.Form("oranges")
%>
<form method="post" action="insert.asp">
<input type="hidden" name="apples" value="<%=iApples%>" />
<input type="hidden" name="oranges" value="<%=iOranges%>" />
<table>
<tr>
<th colspan="5">Apples</th>
</tr>
<tr>
<td>&nbsp;</td>
<td>net price</td>
<td>gross price</td>
<td>date</td>
<td>time</td>
</tr>
<% For i = 1 To iApples %>
<tr>
<td>Apples <%=i%></td>
<td><input name="Applesnet<%=i%>" type="text" /></td>
<td><input name="Applesgross<%=i%>" type="text" /></td>
<td><input name="Applesdate<%=i%>" type="text" /></td>
<td><input name="Applestime<%=i%>" type="text" /></td>
</tr>
<% Next %>

<tr>
<th colspan="5">Orange</th>
</tr>
<tr>
<td>&nbsp;</td>
<td>net price</td>
<td>gross price</td>
<td>date</td>
<td>time</td>
</tr>
<% For i = 1 To iOranges %>
<tr>
<td>Apples <%=i%></td>
<td><input name="Orangesnet<%=i%>" type="text" /></td>
<td><input name="Orangesgross<%=i%>" type="text" /></td>
<td><input name="Orangesdate<%=i%>" type="text" /></td>
<td><input name="Orangestime<%=i%>" type="text" /></td>
</tr>
<% Next %>
</table>
<input type="submit" />
</form>
</body>
</html>


--------------------------------------
INSERT.ASP:
<%
Dim iOranges, iApples
iApples = Request.Form("apples")
iOranges = Request.Form("oranges")

Set oADO = CreateObject("ADODB.Connection")
oADO.Open YourConnectionStringHere
For i = 1 To iApples
sSQL = "INSERT INTO [apples] ([net],[gross],[date],[time]) VALUES (" & _
Request.Form("Applesnet" & i) & "," & _
Request.Form("Applesgross" & i) & "," & _
Request.Form("Applesdate" & i) & "," & _
Request.Form("Applestime" & i) & ")"
' oADO.Execute sSQL,,129 UNCOMMENT IF ALL IS FINE
RESPONSE.WRITE SSQL & "<BR>"
Next
For i = 1 To iOranges
sSQL = "INSERT INTO [Oranges] ([net],[gross],[date],[time]) VALUES (" & _
Request.Form("Orangesnet" & i) & "," & _
Request.Form("Orangesgross" & i) & "," & _
Request.Form("Orangesdate" & i) & "," & _
Request.Form("Orangestime" & i) & ")"
' oADO.Execute sSQL,,129 UNCOMMENT IF ALL IS FINE
RESPONSE.WRITE SSQL & "<BR>"
Next
oADO.Close : Set oADO = Nothing
%>

Ray at home
"Nick" <an*****@gmail.com> wrote in message
news:c3**************************@posting.google.c om...
Loop to create an array from a dynamic form.

I'm having trouble with an application, and I'll try to explain it as
clearly as possible:

1. I have a form with two fields, say Apples and Oranges. The user
selects from a drop down menu, between 1-10 of each and clicks submit.
The resulting page will display a NEW form, with rows and a list of
fields for the amount of each items selected.

Example. If I selected 3 apples and 5 oranges, the resulting page
would show 3 rows of text fields for apples and 5 rows of text fields
for oranges.

The form fields would be (after each semicolon, the first word is the
form element name (Apple1, Apple2)):

Apple1: net price | gross price | date | time
Apple2: net price | gross price | date | time
Apple3: net price | gross price | date | time
Orange1: net price | gross price | date | time
Orange2: net price | gross price | date | time
Orange3: net price | gross price | date | time
Orange4: net price | gross price | date | time
Orange5: net price | gross price | date | time
I want to insert each one of these records into the database, each
record being a net, gross price, date and time for apple 1-X and
orange 1-X. The apples will go into the apple table and the oranges
will go into the oranges table.
I'm used to looping like this:

for each item in request.form
if left(item, 6) = "orange" then
orange = orange & request.form(item) & ","
end if
next
After that, I'll create a loop that inserts each record into the
database separately, something of this nature:
for each item in orange
set rsOrange= server.createobject("adodb.recordset")
strSql = "select * from oranges"
rsOrange.open strSql, Connstr, 1, 3

rsOrange.Addnew
rsOrange("net") = request.form("net")
rsOrange("gross") = request.form("gross")
rsOrange("date") = request.form("date")
rsOrange("time") = request.form("time")
rsOrange.Update
rsOrange.close
next
But since this is a multi dimensional array (gross, net, date, time
will need to be inserted correspondingly), you can see my dilemma. I
understand how they work when hard coded, but not to dynamically
create and populate them, then use in an insert statement. Sorry if
this is elementary, but any help would really be appreciated.

Thanks!

Jul 19 '05 #2
N I
Ray
Thanks so much. I had created the form elements as you described in your
reply, but your insert loop is perfect and much more simple than what I
had started working with.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
4333
by: John Wilson | last post by:
Hello, I have the following code which populates as table data from a SQL Server 2000 stored proc (RSByDemoID2). Below that is the view and stored procedure which takes @DemoID as input to match...
1
1462
by: Øyvind Isaksen | last post by:
Hello! I need to dynamic generate a SQL statement based on how many images a user select to upload. Here you see an example with 2 images. It can be up to 50 images and I dont want to write...
4
18789
by: Kevin H | last post by:
Apologies in advance if this sounds slow-witted, but I didn't find it here. Need to populate some textboxes on a form. While I could hard code it (the number of options aren't that high), it...
8
2843
by: Hardrock | last post by:
I encountered some difficulty in implementing dynamic loop nesting. I.e. the number of nesting in a for(...) loop is determined at run time. For example void f(int n) { For(i=0; i<=K; i++)...
4
4318
by: Bill Sun | last post by:
Hi, All I have a conventional question, How to create a 3 dimension array by C language. I see some declare like this: int *** array; int value; array = create3Darray(m,n,l);
6
4728
by: Rich | last post by:
Hello, I want to simulate the dynamic thumbnail display of Windows Explorer (winxp) on a form or pannel container. If I place a picture box on my container form/pannel and dimension it to the...
7
1853
by: murrayatuptowngallery | last post by:
After a few searches & inquiries here, I have a method of generating an html table populated with results of equations that works and wasn't too far up a learning curve for me, thanks to a helpful...
22
26664
by: silversurfer2025 | last post by:
Hello everybdy, I am a little confused for the following reason: In my code I used a simple for-loop in order to initialize a 2D-array of floats to zero. Because of efficiency reasons, I changed...
1
2432
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
0
7021
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7207
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6914
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7401
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4928
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3114
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3112
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1434
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
318
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.