Click to See Complete Forum and Search --> : variable names
bloke
05-28-2003, 10:17 AM
Chaps
Can I dynamically create a variable name!? Or to be more specific:
strperson = "Robert Englebert Hoskins"
PersonArray = split(strperson ," ")
For iLoop = lBound(PersonArray) to UBound(PersonArray)
dynamicallycreatedvariablename = PersonArray(iLoop)
response.write dynamicallycreatedvariablename&"<br>"
Next
dynamicallycreatedvariablename being something like namepart0, namepart1 etc, adding the value of iLoop to the end of the variable name.
The problem being not knowing how many name parts may be in strperson (Bob, Bob Hoskins, Bob Englebert Hoskins etc)
This makes perfect sense in my head!
Cheers
Ribeyed
05-28-2003, 08:15 PM
hi,
if you dynamically create form elements so you don't know the exact name then you can use request.form(item) to refer to the form element. You can try use item as the variable name which should name the variable the same as the dynamic form element, if you get what i mean.
example:
<%
countloop = 5
for x = 1 to countloop
%>
<input type="hidden" name="hiddenField<%=x%>" value="somethingdynamic">
<%
Next
%>
'so contiuned in your code or on another page you can do this:
<%
for each item in request.form
item = request.form(item)
next
%>
Hope this helps
Bullschmidt
05-29-2003, 02:57 AM
You may want to check out the following:
Using the Eval and Execute Functions - 3/3/2000
http://www.4guysfromrolla.com/webtech/030300-1.shtml
bloke
05-29-2003, 03:39 AM
Originally posted by Dave Clark
The direct answer is, "No." But, I also cannot see why you would want to do that in the first place. Can you explain a little better what the advantage would be to do that? Or, why you need to know how many parts to the name in the first place?
Because UBound() + 1 will tell you how many parts altogether.
Dave
The reason I want to do it is I need to break down a name typed into a form by a user and compare the parts against names in a database. For example, a user may type Bob Hoskins however the name may be stored in the database as Bob Englebert Jeremiah Hoskins. Simply comparing the form entry against the database will return no results.
If I break the form entry down into name parts, I can look for the names which contain Bob AND Hoskins.
Make any sense?!
Ribeyed
05-29-2003, 08:09 AM
Hi,
in the sql select statment use "LIKE"
bloke
05-29-2003, 08:18 AM
Yes, that's fine if the user just enters 'Bob' but if the database entry is 'Bob Englebert Hoskins' and the user enters 'Bob Hoskins' then you're scuppered.
Anyway, I think I have solved it. Bit messy but I've done this:
PersonArray = split(strperson ," ")
strpersonstring = "sp_get_person '"
For iLoop = lBound(PersonArray) to UBound(PersonArray)
strpersonstring = strpersonstring & PersonArray(iLoop)
strpersonstring = strpersonstring & "','"
Next
intLength = cint(len(strpersonstring)-2)
strpersonsearch = left(strpersonstring, intLength)
Then assumed there will be no more than 4 name parts and used the following sp:
CREATE PROCEDURE sp_get_person
@namepart1 varchar(50)= null,
@namepart2 varchar(50)= null,
@namepart3 varchar(50)= null,
@namepart4 varchar(50)= null
AS
select distinct forename, surname, contsearch, networklogon
from companypeople
where (RTRIM(forename) + ' ' + LTRIM(surname))
Like '%'+RTRIM(@namepart1+' '+IsNull(@namepart2, ' ')+' '+IsNull(@namepart3, ' ')+' '+IsNull(@namepart4, ' '))+'%'
GO
Horrid, I know, but seems to do the trick!
Cheers for you help anyway.
bloke
05-29-2003, 08:28 AM
Cool.
I guess I've just been mucking around with it for too long and started to go a bit cross-eyed!
I need a beer!
Cheers
bloke
05-29-2003, 08:39 AM
Certainly!
dim gstrConn
gstrConn = "DRIVER={SQL SERVER};SERVER=localhost;DATABASE=yourdatabase;UID=youruid;PWD=yourpassword"
set connection = server.createobject("adodb.connection")
connection.open gstrConn
set rsPerson = server.createobject("adodb.recordset")
rsPerson.Open strpersonsearch ,connection
bloke
05-30-2003, 04:03 AM
Not sure about that mate, never used that method.
What I normally do is:
strprocedurestring = "sp_your_procedure '"
strprocedurestring = strprocedurestring & value1 & "','"
strprocedurestring = strprocedurestring & value2 & "'"
etc etc
Then
set connection = server.createobject("adodb.connection")
connection.open gstrConn
set rslogin = server.createobject("adodb.recordset")
rslogin.Open strprocedurestring ,connection
The only other way I've ever used is:
<%
data_source = gstrConn
strprocedurestring = "sp_your_procedure " & "'" & request.form("value1") & "','"
strprocedurestring = strprocedurestring & replace(request.form("value2"), "/", "_") & ".pdf','"
strprocedurestring = strprocedurestring & nullNA(request.form("value3")) & "'"
etc etc
Set con = Server.CreateObject("ADODB.Connection")
con.Open data_source
con.Execute strprocedurestring
con.Close
Set con = Nothing
%>
Hope this helps!