Here it is modified to handle more than one ... just enter a comma
delimited string into the function. if only one username, don't use a
comma.
<%
Function userLink(theUserNameList)
Dim theUsernameSeperator
theUsernameSeperator = "<b />"
Dim theUserID, theSQLStatement, linkSentence
Dim theUsernameArray, theUserName, n
theUsernameArray = Split(theUserNameList, ",")
For n = 0 to ubound(theUsernameArray)
theUserName = theUsernameArray(n)
theSQLStatement = "Select sUserGUID From tblUsers "
theSQLStatement = theSQLStatement & "Where sUserName = '" &
theUserName & "';"
Set rsName = db.Execute(theSQLStatement)
If Not rsName.EOF Then '//User Exists
theUserID = rsName("sUserGUID")
linkSentence = "<a href=""userpage.asp"
linkSentence = linkSentence & "?userid=" & theUserID & """>"
linkSentence = linkSentence & theUserName
linkSentence = linkSentence & "</a>" & theUsernameSeperator
Else '//User Doesn't Exist
linkSentence = "**Invalid username ( " & & " )**"
End If
Next
rsName.Close
Set rsName = Nothing
userLink = linkSentence
End Function
Response.Write "This will be a link to " & userLink("Hagge")
%>
I could make this faster using my DBConn.asp script. I would build a
comma delimited list with each of the usernames between 2 single
quotes. then I would use my cp_sqlArray function with a "select where
in" statement so that you are only searching the database once. The
code would look something like this
I would also change this into a subroutine because performance will be
better writing the code than building a string
<!-- #include virtual="/coolpier_scripts/_database_tools/DBConn.asp"
-->
<%
Sub userLinks(theUserNameList)
'// Create the usersList for the sql in statement
Dim usersList, usersArray
usersList = "''" & Replace(theUserNameList, ",",
"'',''") & "''"
'// Get a 2-dimensional array of the results
theSQL = "Select sUserGUID, sUserName " &_
"From tblUsers " &_
"Where sUserName IN (" & usersList & ");"
usersArray = cp_sqlArray(theSQL)
'//DISPLAY
With Response
If IsArray(usersArray) Then
Dim theRows '//
For theRows = 0 to ubound(usersArray,1)
.Write "<a href=""userpage.asp"
.Write "?userid=" & usersArray(0,n) & """>"
.Write usersArray(1,n) & "</a>"
.Write "<br />"
Next
Else
.Write "No Users Found!"
End If
End With
End Sub
cp_TheConnectionString = "yourConnectionString"
cp_DBConn("open")
Call userLinks("Hagge, Smith")
cp_DBConn("close")
%>
But say you type in...
[userid=Hagge] and [userid=Smith]
Will it convert both then, or just the first match?
// Hagge
Brynn said:
Just to help a little. There is no reason to have to build an array or
have any loop for what you want to do with your function. The below
should do what you are wanting to do, and much faster and easier to
use.
Response.Write "This will be a link to" & userLink("Hagge")
will return...
This will be a link to <a href="userpage.asp?userid=XXXXXX">Hagge</a>
I added also ... if Hagge is not in the usersTable ... it will return
This will be a link to **Invalid username**
Anyway, I hope the below function helps ... if yours is working, this
one should work with no changes in my function. Also, it will be much
easier to call since you are giving it the username only. The function
only returns the link.
<%
Function userLink(theUserName)
Dim theUserID, theSQLStatement, linkSentence
theSQLStatement = "Select sUserGUID From tblUsers "
theSQLStatement = theSQLStatement & "Where sUserName = '" &
theUserName & "';"
Set rsName = db.Execute(theSQLStatement)
If Not rsName.EOF Then '//User Exists
theUserID = rsName("sUserGUID")
linkSentence = "<a href=""userpage.asp"
linkSentence = linkSentence & "?userid=" & theUserID & """>"
linkSentence = linkSentence & theUserName
linkSentence = linkSentence & "</a>"
Else '//User Doesn't Exist
linkSentence = "**Invalid username**"
End If
rsName.Close
Set rsName = Nothing
userLink = linkSentence
End Function
Response.Write "This will be a link to " & userLink("Hagge")
%>
Brynn
www.coolpier.com
I solved it, this is my function to be used like this...
Response.write userLink("This will be a link to [userid=Hagge]")
Will be displayed as...
This will be a link to <a href="userpage.asp?userid=XXXXXX">Hagge</a>
Function userLink(sText)
sFuncText=""
Dim arrayWords(20000)
arrWords = Split(sText," ")
For iWords=0 to Ubound(arrWords)
iStartLength = Int("0")
iEndLength=0
iStartLength = Int(Instr(arrWords(iWords),"[userid=")+8)
iEndLength = Int(Instr(arrWords(iWords),"]"))
iNameLength = Int(iEndLength-iStartLength)
If iNameLength > Int("0") Then
sFuncUserName = Mid(arrWords(iWords),iStartLength,iNameLength)
SQL=""
SQL=SQL & "SELECT sUserGUID"
SQL=SQL & " FROM tblUsers"
SQL=SQL & " WHERE sUserName='" & sFuncUserName & "'"
Set rsName = db.execute( SQL )
If NOT rsName.EOF Then
sFuncText=sFuncText & "<a href=""userpage.asp?userid=" &
rsName("sUserGUID") & """>" & sFuncUserName & "</a>"
End If
rsName.Close
Set rsName = Nothing
Else
sFuncText=sFuncText & arrWords(iWords) & " "
End If
Next
userLink = sFuncText
End Function
// Hagge
I'm stuck in a problem
Hi I want to create a funtion in asp.
Like this [userid=Smith] Then the function convert the "custom tag" to.
<a href=
www.mypage.com/user/Smith>Smith</a>
How to do?
I'm real stucked.
// Hagge