Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    problem with loop

    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

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    try this:

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

  • #3
    Senior Coder
    Join Date
    Dec 2002
    Location
    Arlington, Texas USA
    Posts
    1,065
    Thanks
    4
    Thanked 8 Times in 8 Posts
    Or you can use
    Code:
    For Each item In Request.Form
           Response.Write item & ": " & Request.Form(item) & "<br>"
    Next

  • #4
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    yes, well you can do that with either of those examples.

    just build your insert string dynamicly.

  • #6
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    Mirdana's example was a good one to work from, i actually haven't seen it done like that before.

    PHP Code:
    <%
    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.
    Last edited by angst; 10-11-2007 at 05:06 PM.

  • #7
    Regular Coder
    Join Date
    Mar 2007
    Posts
    505
    Thanks
    1
    Thanked 19 Times in 19 Posts
    So then run the first and last parts of the SQL statement outside the loop.

    Code:
    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
    Last edited by Daemonspyre; 10-11-2007 at 08:20 PM.
    To say my fate is not tied to your fate is like saying, 'Your end of the boat is sinking.' -- Hugh Downs
    Please, if you found my post helpful, pay it forward. Go and help someone else today.

  • #8
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    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.

  • #9
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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) & ")"

  • #10
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    <%
    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) )
    %>

  • #11
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #12
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    hmm, you would need to test it to work out any issues, but I'm sure that could be done.

  • #13
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #14
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •