Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Concatenate 1

Status
Not open for further replies.

A1Pat

IS-IT--Management
Jun 7, 2004
454
US
Hello all,

A little explaination:
I have 10 repetitive rows for user to enter 10 different names. After entering, information will be saved into database. I'm finding a way to do as such without using the same code for all 10 rows.

The bottom is what I come up with by utilizing the FOR NEXT loop for "prodName" variable + "iCounter" to store 10 names that entered.

Question 1: Will this works? If so,
Question 2: how can I concate in such situation?

For iCounter = 1 to 10

prodName_iCounter = Replace(Server.HTMLEncode(Request ("prodName_iCounter")), "'", "''")

Next

Thanks for any help.
 
if you use a good naming convention on the form elements like :

name_1
address_1
city_1
zip_1

name_2
address_2
etc etc

then you can just do...

for i=1 to 10
Names = Names & request("name_" & i)
addrs = addrs & request("address_" & i)
cities = cities & request("city_" & i)
zips = zips & request("zip_" & i)
Next

then these can be used as arrays or whatnot for inserting into the db .. or

for i=1 to 10
SQL = "insert into table(name, address, city, zip) values('" & request("name_" & i) & "','" & request("address_" & i) & "','" & request("city_" & i) & "','" & request("zip_" & i) & "')"
connection.execute(sql)
Next


hope the info helps

[thumbsup2]DreX
aKa - Robert
if all else fails, light it on fire and do the happy dance!
 
Thanks Drex for the help.

Now, a little more advance to my problem:

Can I loop through the input data to find out which rows are filled and only take info from those fields of the rows to save.

For ex: user could fill out info in row 1, row2, row5, etc.
in that case, row 3 and row4 are empty and I don't want to save those empty rows.

Thanks again.
 
just add a value check conditional in the for next

also forgot to note, you might have to alter the naming convention supplied if you're using client side java validation, java really doesn't like elements with numbers in the names, and underscores. just fyi and forewarning :)


as for the conditional .. probably would be a little easier on the eyes to do something like this ( please note the built up variables are in SQL formatting so to directly inject them into an SQL statement later, some single quote handling might be recommended as well ) :

Code:
for i=1 to 10 
  Fields = ""
  Values = "" 
  If Request("name_" & i) <> "" Then
    Fields = Fields & "Name,"
    Values = Values & "'" & request("name_" & i) & "',"
  End If
  If Request("Address_" & i) <> "" Then
    Fields = Fields & "Address,"
    Values = Values & "'" & request("address_" & i) & "',"
  End If
  If Request("City_" & i) <> "" Then
    Fields = Fields & "City,"
    Values = Values & "'" & request("city_" & i) & "',"
  End If
  If Request("Zip_" & i) <> "" Then
    Fields = Fields & "Zip,"
    Values = Values & request("Zip_" & i) & ","
  End If
  
  If Fields <> "" and Values <> "" Then
    Fields = Left(Fields,Len(Fields)-1) ' killing trailing commas
    Values = Left(Values,Len(Values)-1)
    SQL = "Insert into Table(" & fields & ") Values(" & values & ")"
    Connection.Execute(SQL)
  End If
Next
now you could make it less easy on the eyes and make some arrays of like element to field name maps, data types etc, and use these in the loop to fire off in sub loop saving having to make large if blocks each time you add a field, instead of just adding the element/field to the arrays and calling it good.

as it stands it's pretty easy to follow vs some intricate array mess, so maybe might be easier to stick to this format. also note the use of ZIP it's handled numeric in case that crossed your mind :)

hope that helps.

[thumbsup2]DreX
aKa - Robert
if all else fails, light it on fire and do the happy dance!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top