2

I'm sending a string to a page that looks like this:

CalMiles_MS.asp?s0=Chicago,IL&s1=Akron,OH&s2=,EMPTY&s3=,EMPTY&s4=,EMPTY&s5=,EMPTY&s6=,EMPTY&s7=,EMPTY&s8=,EMPTY&s9=,EMPTY&s10=,EMPTY&s11=,EMPTY&s12=,EMPTY&s13=Des Moines,IA&s14=,EMPTY&s15=,EMPTY&s16=,EMPTY&s17=,EMPTY&s18=,EMPTY&s19=,EMPTY&s20=,EMPTY&s21=,EMPTY&s22=Miami,FL&s23=Dallas,TX

but in that "CalMiles_MS.asp" page I would like to remove all of the ",EMPTY" and re-order the "&s" + Number(s) so when complete the above example would look like this:

CalMiles_MS.asp?s0=Chicago,IL&s1=Akron,OH&s2=Des Moines,IA&s3=Miami,FL&s4=Dallas,TX

How can I do that ?

Please help, Thanks.

EDIT:

<%
Dim key
Dim qstr
Dim count: count = 0

Response.Write "<p>Input:<br />" & Request.Querystring & "</p>"

for each key in Request.Querystring
    If Request.Querystring(key) <> ",EMPTY" Then
      qstr = qstr & "&s" & count & "=" & Request.Querystring(key)
        count = count + 1
    End If
Next

If qstr <> "" Then
    qstr = Right(qstr, Len(qstr) - 1)
End If

Response.Write "<p>Output:<br />" & qstr & "</p>"
%>

But the problem now is NOT that it does not keep the S's (S#) in order, that works fine now, but the City,States are not in the correct order:

 &s1......,.......&s2......,.......&s3......,.......

How can I sort them numerically and keep the order the same (city,state) ?

My Test Query Input:

s0=Columbus,OH&s1=,EMPTY&s2=,EMPTY&s3=Chicago,IL&s4=,EMPTY&s5=,EMPTY&s6=,EMPTY&s7=,EMPTY&s8=Akron,OH&s9=,EMPTY&s10=,EMPTY&s11=Plainfield,IN&s12=,EMPTY&s13=Miami,FL&s14=,EMPTY&s15=,EMPTY&s16=,EMPTY&s17=,EMPTY&s18=,EMPTY&s19=,EMPTY&s20=,EMPTY&s21=Memphis,TN&s22=Denver,CO&s23=Dallas,TX

Output:

s0=Columbus,OH&s1=Chicago,IL&s2=Miami,FL&s3=Plainfield,IN&s4=Dallas,TX&s5=Memphis,TN&s6=Akron,OH&s7=Denver,CO

but the output should be:

 s0=Columbus,OH&s1=Chicago,IL&s2=Akron,OH&s3=Plainfield,IN&s4=Miami,FL&s5=Memphis,TN&s6=Denver,CO&s7=Dallas,TX
4
  • You need to build the query string manually and post it, perhaps on the click of a button. The question I have to ask is why? It's easier to have your code behind handle the missing value than build the string to be posted. Commented Dec 11, 2013 at 9:35
  • 2
    @Westie - Classic ASP doesn't have a code behind. Commented Dec 11, 2013 at 17:14
  • @Tim: that all depends on how you handle and organise your code. I tend to separate out my Classic ASP code from my mark-up, hence 'code behind'. I tend to name the file filename.code.asp and put it into a folder called 'codeBehind'. Call it a working preference. Commented Dec 12, 2013 at 10:33
  • Wow! +2 on Tim there! LOL! Just goes to show that some people don't believe in code organisation. Commented Dec 12, 2013 at 16:53

2 Answers 2

2

Why don't you loop the query string check for EMPTY then build the string up again?

EDIT: I tested my code and the parameters come out ordered 1,2,3,...

<%
Dim key
Dim qstr
Dim count: count = 0

Response.Write "<p>Input:<br />" & Request.Querystring & "</p>"

for each key in Request.Querystring
    If Request.Querystring(key) <> ",EMPTY" Then
      qstr = qstr & "&s" & count & "=" & Request.Querystring(key)
        count = count + 1
    End If
Next

If qstr <> "" Then
    qstr = Right(qstr, Len(qstr) - 1)
End If

Response.Write "<p>Output:<br />" & qstr & "</p>"
%>

Input:

s0=Chicago,IL&s1=Akron,OH&s2=,EMPTY&s3=,EMPTY&s4=,EMPTY&s5=,EMPTY&s6=,EMPTY&s7=,EMPTY&s8=,EMPTY&s9=,EMPTY&s10=,EMPTY&s11=,EMPTY&s12=,EMPTY&s13=Des%20Moines,IA&s14=,EMPTY&s15=,EMPTY&s16=,EMPTY&s17=,EMPTY&s18=,EMPTY&s19=,EMPTY&s20=,EMPTY&s21=,EMPTY&s22=Miami,FL&s23=Dallas,TX

Output:

s0=Chicago,IL&s1=Akron,OH&s2=Des Moines,IA&s3=Dallas,TX&s4=Miami,FL

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the help... I tired what you wrote me above and I had to change a couple of things but around and I got it to work but the &S numbers are out of order how do I get them to lineup (1,2,3,4,5).. I wrote your modified code below my question. IF YOU CAN PLEASE LOOK AT IT AND SE WHAT I'M DOING WRONG. THANKS!
Hi I tested my code and it works the way you want. Probably your are missing something. Edited my answer for easier testing. Just put it in a test.asp file and call it with your querystring parameters.
Hi, I copied your example in a test.asp and the output is still wrong. I updated my question and below you will see the output I'm getting and the output it should be, please if you can look at it and see what I'm doing wrong... Thanks again for everything.
1
+50

Instead of for each, you need to use a numeric indexer to access the collection. The latter preserves the order of the input string.

<%
Dim qstr
Dim count: count = 0
Dim x

Dim value

for x = 1 To Request.Querystring.Count
    value = Request.Querystring.Item(x)

    If value <> ",EMPTY" Then
        qstr = qstr & "&s" & count & "=" & value
        count = count + 1
    End If
Next

If qstr <> "" Then
    qstr = Right(qstr, Len(qstr) - 1)
End If

Response.Write "<p>Input:<br />" & Request.Querystring & "</p>"
Response.Write "<p>Output:<br />" & qstr & "</p>"
%>

Input:

s0=Columbus,OH&s1=,EMPTY&s2=,EMPTY&s3=Chicago,IL&s4=,EMPTY&s5=,EMPTY&s6=,EMPTY&s7=,EMPTY&s8=Akron,OH&s9=,EMPTY&s10=,EMPTY&s11=Plainfield,IN&s12=,EMPTY&s13=Miami,FL&s14=,EMPTY&s15=,EMPTY&s16=,EMPTY&s17=,EMPTY&s18=,EMPTY&s19=,EMPTY&s20=,EMPTY&s21=Memphis,TN&s22=Denver,CO&s23=Dallas,TX

Output:

s0=Columbus,OH&s1=Chicago,IL&s2=Akron,OH&s3=Plainfield,IN&s4=Miami,FL&s5=Memphis,TN&s6=Denver,CO&s7=Dallas,TX

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.