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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

breaking up a querystring - PLEASE HELP!!! 1

Status
Not open for further replies.

cm80

Technical User
May 3, 2001
85
US
Hi,
I hope somone can help me with this problem.

I want to break a querystring into name value pairs.
For example I have a querystring like

mypage.asp?Name=Scott&address=someplace&Phone=12345&zip=456

and I want to break it up so that the result is a list like

Name=Scott
Address=Someplace
Phone=12345
Zip=456

or better yet, just

Scott
Someplace
12345
456

So far I have the following code, which for the querystring above would show a message saying '4 Pairs'
########
Private Sub Command1_Click()
Dim iLen As Integer
Dim strWork As String
Dim ptr As Integer
Dim strET As String
Dim iNameValueCount As Integer

strET = Me.Text1.Text

For ptr = 1 To Len(strET)
strWork = Mid(strET, ptr, 1)
If strWork = "&" Or strWork = "?" Then iNameValueCount = iNameValueCount + 1
Next
MsgBox iNameValueCount & " pairs"
End Sub
########
Can anyone help me continue from here please?
 
Hi cm80,

I'm not sure I understand what it is you want to do? You can query the querystring with:

Code:
sValue = request.querystring("Name")

sValue will contain "Scott"

If you want to parse the querystring to another function and just get the parameters and the values in an array, you could do something like this:

Code:
Dim sQuerystring As String
Dim sParts() As String
Dim iParts As Integer
Dim i As Integer
Dim iEqualSign As Integer
Dim sResults() As String
Dim sKey As String
Dim sValue As String


sQuerystring = "Name=Scott&address=someplace&Phone=12345&zip=456"

'Split splits the string and stores the seperate
'values in an zoro-based array
sParts = Split(sQuerystring, "&")

'How many pairs.
iParts = UBound(sParts)
ReDim sResults(iParts, 1)

For i = 0 To iParts
   iEqualSign = InStr(1, sParts(i), "=")
   sKey = Left$(sParts(i), iEqualSign - 1)
   sValue = Right$(sParts(i), Len(sParts(i)) - iEqualSign)
   
   sResults(i, 0) = sKey
   sResults(i, 1) = sValue
Next i

Erase sParts
'sResults contains your querystring now.

Hope this helps you!

Jordi Reineman
 
Thanks Jordi,
I used the second part and it worked perfectly.
I now have another problem though.

As well as the '&', my querystrings can also contain'%' and I want to be able to separate it based on this also.
ie, get the values between the '%' symbols.
Is there someway that I can change the 'Split' statement to include both symbols.
Please, and thanks again. That was great!
 
Hi cm80,

I hope you didn't include these % yourself, because as you probably know, in an http string they resamble a space. To my knowledge you can't use the split function to split on two different searchstrings, but what you can do is first split them like in the example and then search in each value if an % occurs and if so split the value.

But again it's not all clear to me why you would want to do so. Perhaps you could me more specific to why you wnat to split the querystring in the first place.

Good luck!

Jordi Reineman
 
ehhhhh....

%20 equals a space of course...

Jordi Reineman
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top