...

View Full Version : problem with loop



simba2be
10-11-2007, 03:56 PM
Hi,

Been searching for a long time for the following problem.

Form generated with a loop no problem.
But now to get to the result.

fields like name1, tel1, name2, tel2 etc.

Now when i collect this info on the next page to input it to the db, i would like to receive the following.

'factor is a previous geneate number' for e.g. 5
---
Dim i
...

Do While i <= factor
naam(i) = request.form("naam" & i)
i = i + 1
Loop


But i get errors, is there any way to accomplish this.

Thanks

angst
10-11-2007, 04:15 PM
try this:



strFormArray = Split(Trim(request.Form),"&")
For x = 0 to UBound(strFormArray)
strFormString = Replace(strFormArray(x),"+"," ")
strData = Split(strFormString,"=")
response.write strData(0) & ": " & strData(1) & "<br />"
Next

miranda
10-11-2007, 04:41 PM
Or you can use


For Each item In Request.Form
Response.Write item & ": " & Request.Form(item) & "<br>"
Next

simba2be
10-11-2007, 04:56 PM
Yes that would work if i just want to output it on screen but later on i would like to add into a seperate table in SQL...


like
if name(i)<>"" then
SQLstmt = "INSERT INTO Hermans.persoon(name, tel)"
SQLstmt = SQLstmt & " VALUES ("
SQLstmt = SQLstmt & "'" & name(i) & "',"
...

end if

etc

angst
10-11-2007, 04:58 PM
yes, well you can do that with either of those examples.

just build your insert string dynamicly.

angst
10-11-2007, 05:03 PM
Mirdana's example was a good one to work from, i actually haven't seen it done like that before.



<%
For Each item In Request.Form
strInsert = strInsert & item & " = '" & Request.Form(item) & "',"
Next
oConn.execute("INSERT INTO SomeTable SET " & left(strInsert,len(strInsert) -1) )
%>


just make sure that your form field names are the same as your table field names.

Daemonspyre
10-11-2007, 05:05 PM
So then run the first and last parts of the SQL statement outside the loop.


SQLstmt = "INSERT INTO Hermans.persoon (name, tel) VALUES ("
'
for each item in request.form
if request.form(item) <> "" then
SQLstmt = SQLstmt & "'" & request.form(i) & "',"
else
SQLstmt = SQLstmt & "NULL,"
end if
next
'
SQLstmt = LEFT(SQLstmt,LEN(SQLstmt)-1) & ")"

EDIT 2: angst -- You are correct - I should have double-checked my code. My comment was not meant to cause anger, but to use the ANSI SQL standard. Please accept my apologies

angst
10-11-2007, 05:09 PM
no, your wrong, that will work just fine.

but your code wont work.
request.form(i)
where is the "i" coming from? maybe you mean "item" ??

you should check your code before posting.

simba2be
10-11-2007, 05:17 PM
ok but that solution still gives me problems with replace...in some code

for e.g.

tel= datfom(tel) external script for transforming tel code, or for example a
price= replace(price,",",".")


SQLstmt = "INSERT INTO Hermans.persoon (name, tel, price) VALUES ("
'
for each item in request.form
if request.form(i) <> "" then
SQLstmt = SQLstmt & "'" & request.form(i) & "',"
else
SQLstmt = SQLstmt & "NULL,"
end if
next
'
SQLstmt = LEFT(SQLstmt,LEN(SQLstmt)-1) & ")"

angst
10-11-2007, 05:20 PM
<%
For Each item In Request.Form
if Not Request.Form(item) = "" Then strInsert = strInsert & item & " = '" & replace(Request.Form(item),",",".") & "',"
Next
oConn.execute("INSERT INTO SomeTable SET " & left(strInsert,len(strInsert) -1) )
%>

simba2be
10-11-2007, 05:32 PM
Does this mean i can execute the following too since some form elments are for a different table, and some elment don't need a replace.

<%
For Each item In Request.Form
if Not Request.Form("name").Item = "" Then strInsert = strInsert & ("name").item & " = '" & Request.Form(item) & "'," &
strInsert = strInsert & ("tel").item & " = ' & replace(Request.Form(item),",",".") & "',"
Next
oConn.execute("INSERT INTO SomeTable SET " & left(strInsert,len(strInsert) -1) )

Sorry thanks for the help

angst
10-11-2007, 05:36 PM
hmm, you would need to test it to work out any issues, but I'm sure that could be done.

simba2be
10-11-2007, 05:55 PM
doesn't work result just nothing. no error

tried towards screen with sigle line but also no result.

don't forget the form fields are name1, name2, name3 etc each need to output to a different row in the same sql database.

tried:

For Each item In Request.Form
if Not Request.Form("name").item = "" Then Response.Write item & ": " & Request.Form(item) & "<br>"
next

simba2be
10-11-2007, 07:46 PM
Maybe i need to precise a couple of things:

So i have a main form: company, street etc..

In the form is a + sign every time theu click this they can add another person

for ex: name1: Jef
price1: 1,2
name2: Mike
price2: 3,4

ok till here no prob.

Then i woul like to collect them next page:

company=request.form("company")
street= ...

add this to table company - SQL no problem

but then i want to add name1, tel1 to another table called person in one row
then name2, tel2 to another row, etc...

but it could be 50 people or more...
now i can get the amount of inputs on the main form, but then i'm stuck with getting the name1, name2 etc fiels and adding them into the dbase



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum