SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard westmich's Avatar
    Join Date
    Mar 2000
    Location
    Muskegon, MI
    Posts
    2,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Finding the Query String names also

    The code below displays the query string values. How do I grab the names, on the fly, if I don't know them ahead of time.
    Code:
    <%
    Dim item
    For each item in Request.QueryString
    	Response.Write(Request.QueryString(item) & "<br>" & vbcrlf)
    Next
    %>
    Westmich
    Smart Web Solutions for Smart Clients
    http://www.mindscapecreative.com

  2. #2
    SitePoint Wizard
    Join Date
    Jan 2001
    Location
    Milton Keynes, UK
    Posts
    1,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You've answered your own question.

    The item variable in the code you posted actually holds the name, which is then used as the key for the querystring collection to get the associated value.

    As far as I know there isn't a built in method to get a string/array of the names but you can loop around the QueryString collection getting the names.

    The code below demonstrates this by creating 2 arrays to hold the names and values in the querystring collection.

    A loop is then processed to output the array names and values.

    Code:
    Dim intCount
    Dim i
    Dim arrItems
    Dim arrValues
    
    intCount = Request.QueryString.Count
    ReDim arrItems(intCount - 1)
    ReDim arrValues(intCount - 1)
    
    i = 0
    For Each Item In Request.QueryString
    	arrItems(i) = Item
    	arrValues(i) = Request.QueryString(Item)
    	i = i + 1
    Next
    
    For i = 0 To UBound(arrItems, 1)
    	Response.Write i & " : " & arrItems(i) & " : " & arrValues(i) & "<br>"
    Next

  3. #3
    SitePoint Wizard westmich's Avatar
    Join Date
    Mar 2000
    Location
    Muskegon, MI
    Posts
    2,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the response, Shane. I guess I overlooked the obvious. The '.count' was also useful, didn't know it was there

    However, if I may comment, the counter isn't necassary. The purpose of the For Each ... In loop is to not have to use a counter.
    Code:
    <%
    Dim item
    For each item in Request.QueryString
    	Response.Write(item & " = " & Request.QueryString(item) & "<br>")
    Next
    %>
    Westmich
    Smart Web Solutions for Smart Clients
    http://www.mindscapecreative.com

  4. #4
    SitePoint Wizard
    Join Date
    Jan 2001
    Location
    Milton Keynes, UK
    Posts
    1,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, the loop counter is unnecessary, but I used it just so I could get the names into an array for the demo. Sometimes it may be useful to get the names into an array or string if you're using the names in multiple places in a script.

    However, like you noted, if you're just writing out the name/value pairs of a collection getting the the elements into an array is unnecessary.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •