473,732 Members | 2,171 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(it em) & ","
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.createob ject("adodb.rec ordset")
strSql = "select * from oranges"
rsOrange.open strSql, Connstr, 1, 3

rsOrange.Addnew
rsOrange("net") = request.form("n et")
rsOrange("gross ") = request.form("g ross")
rsOrange("date" ) = request.form("d ate")
rsOrange("time" ) = request.form("t ime")
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 4081
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.as p">
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("a pples")
iOranges = Request.Form("o ranges")
%>
<form method="post" action="insert. asp">
<input type="hidden" name="apples" value="<%=iAppl es%>" />
<input type="hidden" name="oranges" value="<%=iOran ges%>" />
<table>
<tr>
<th colspan="5">App les</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="Applesgro ss<%=i%>" type="text" /></td>
<td><input name="Applesdat e<%=i%>" type="text" /></td>
<td><input name="Applestim e<%=i%>" type="text" /></td>
</tr>
<% Next %>

<tr>
<th colspan="5">Ora nge</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="Orangesne t<%=i%>" type="text" /></td>
<td><input name="Orangesgr oss<%=i%>" type="text" /></td>
<td><input name="Orangesda te<%=i%>" type="text" /></td>
<td><input name="Orangesti me<%=i%>" type="text" /></td>
</tr>
<% Next %>
</table>
<input type="submit" />
</form>
</body>
</html>


--------------------------------------
INSERT.ASP:
<%
Dim iOranges, iApples
iApples = Request.Form("a pples")
iOranges = Request.Form("o ranges")

Set oADO = CreateObject("A DODB.Connection ")
oADO.Open YourConnectionS tringHere
For i = 1 To iApples
sSQL = "INSERT INTO [apples] ([net],[gross],[date],[time]) VALUES (" & _
Request.Form("A pplesnet" & i) & "," & _
Request.Form("A pplesgross" & i) & "," & _
Request.Form("A pplesdate" & i) & "," & _
Request.Form("A pplestime" & 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("O rangesnet" & i) & "," & _
Request.Form("O rangesgross" & i) & "," & _
Request.Form("O rangesdate" & i) & "," & _
Request.Form("O rangestime" & 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.goo gle.com...
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(it em) & ","
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.createob ject("adodb.rec ordset")
strSql = "select * from oranges"
rsOrange.open strSql, Connstr, 1, 3

rsOrange.Addnew
rsOrange("net") = request.form("n et")
rsOrange("gross ") = request.form("g ross")
rsOrange("date" ) = request.form("d ate")
rsOrange("time" ) = request.form("t ime")
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
4353
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 to the event_id. For Q? and Comments I am viewing/updating in a different table than I am question and how_to_answer. The stored proc is populated by a view that I'm using to get all these values from two tables. My quandry is, I am getting the...
1
1473
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 this lines 50 times since they are almost identical (Example, first line has "varImage_1" , the second has "varImage_2"... and It shall go up to varImage_50...).
4
18835
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 would be more compact/efficient to use a loop. However, I haven't found anything that describes how to reference the textboxes sequentially ... evaluating the hard part of the textbox name, with the number of the loop iteration, to address the...
8
2855
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++) For(i=0; i<=K; i++)
4
4335
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
4755
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 size of a thumbnail and set the sizemode to Stretch -- I get one thumbnail. I want to retrieve all the picture files (jpg, bmp) in a directory into an array list and then display this list as thumbnails on my form dynamically. So my question is...
7
1868
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 respondent/poster here... <script type="text/javascript"> var AAA = new Array(4) AAA = 2*0.1*Math.round(10*Math.log(32)/Math.log(2)) AAA = 2*0.1*Math.round(10*Math.log(16)/Math.log(2))
22
26736
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 it to use memset and I get totally different results.. How can this be? Here is the example: float gaborfilter;
1
2448
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 of different lengths (e.g. usually between 25 and 45 rows. The columns in each file have the same length). The text files have been numbered sequentially e.g. cb0, cb1, cb2 and so on. I would like to read the data from each text file into...
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8774
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9447
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9307
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9181
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4550
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
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 we have to send another system

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.