Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 456
  • Last Modified:

replace comma with text, then loop

Hello all,
I have an ASP page that is pulling data from a SQL Server. The SQL server field is a list of codes separated by a comma.

str = AB, BC, DE
this string is of variable length

Dim i, str, end_str,
      i = 1
                str.value = "AB, BC, CD"
      Do
      temp = mid(str.value, instr(str.value,",") + i, len(str.value))
      end_str.value = end_str.value & replace(str.value, ",", "<a href='mypage.asp?code=" & temp & '</a>")
      i = i + 1
Loop While i < len(str.value) + 1

What I would like to do is convert the list of codes to a link.  I tried the code above but i don't seem to get what i need.
Thanks
0
nito8300
Asked:
nito8300
  • 3
  • 3
  • 2
  • +1
3 Solutions
 
deathtospamCommented:
Are you trying to create multiple links, one for each code in [str], like this?

============================================================
<a href="mypage.asp?code=AB">Code AB</a><br>
<a href="mypage.asp?code=BC">Code BC</a><br>
<a href="mypage.asp?code=DE">Code DE</a><br>
============================================================


Or are you trying to pass all three codes in one link, like this?

============================================================
<a href="mypage.asp?code=AB,BC,DE">All three codes</a><br>
============================================================


-= DeathToSpam =-
0
 
deathtospamCommented:
Assuming you want to create one link per code, here's how you would do it:

============================================================
<%
Option Explicit

Dim sListOfCodes, arrCodes, iIndex, sCode, sCodeLink

'sListOfCodes = null
'sListOfCodes = ""
'sListOfCodes = "AB"
'sListOfCodes = "AB, BC"
sListOfCodes = "AB, BC, CD"

If NOT IsNull(sListOfCodes) Then
      arrCodes = Split(sListOfCodes, ", ")

      For iIndex = 0 to UBound(arrCodes)
            sCode = arrCodes(iIndex)
            sCodeLink = "<a href=""mypage.asp?code=" & sCode & """>Link for Code " & sCode & "</a>"
            Response.Write sCodeLink & "<br>"
      Next
End If
%>
============================================================


You can see what happens when the value of [sListOfCodes] changes by only uncommenting the one string you want to use at the top.


-= DeathToSpam =-
0
 
b0lsc0ttCommented:
nito8300,

If I understand you right then do this ...

str = "AB, BC, DE"
arLinks = split(str, ", ")
for each item in arLinks
   Response.Write "<a href=""mypage.asp?code=" & item & """>" & item & "</a>"
next

Let me know how that works.  Let me know if you have any questions or need more information.

b0lsc0tt
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
b0lsc0ttCommented:
dts - A just a little too slow.  Looks like you beat me. :)

bol
0
 
swinslowCommented:

  What it sounds like is you need to separate the values into an array. Since you know all your values are separated with a comma, it makes it much easier
 
  str = AB, BC, DE
  CodeArr = Split(str,",")
  For x = 0 to UBound(CodeArr)
     Response.Write "<a href=" & chr(34)  & "mypage.asp?code=" & CodeArr(x) & chr(34) & ">Click here</a>"
  Next
0
 
nito8300Author Commented:
swingslow:
tried your code, but it only returns the first code.
so if i have AB, BC, CD I only get 'Click here' once and the link is to AB

DTS/b0lsc0tt
will try ur code next.

Thank you all!
0
 
nito8300Author Commented:
swingslow:
i was able to modify ur code to:
mymodule.output.value = mymodule.output.value & "<a href=" & chr(34)  & "mypage.asp?code=" & CodeArr(x) & chr(34) & ">Click here</a>"

that works.
0
 
nito8300Author Commented:
DTS your code works perfect and you answered the question first. thanks everyone!
0
 
b0lsc0ttCommented:
Your welcome!  Thanks for the generous split; it was VERY close. :)  Also, thanks for the fun question, grade and points.  I'm glad I could help.

bol
0
 
swinslowCommented:
Yes, thank you for the split. Looks like we were all trying to answer at  the same time.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now