2010-08-12 49 views
1

我已经很久以前完成了这个工作,现在我找不到该函数。它不应该是太复杂了,但我不知道是否有关于这方面的消息之前,我去再做一遍......经典ASP:查询字符串处理程序

借此:

www.example.com?query=whatever&page=1 

现在想象一下,我按下按钮2页,它将变为:

www.example.com?query=whatever&page=2 

始终保持查询字符串的其余部分保持不变。现在想象一下第2页我按下按钮按日期命令,它应该变成:

www.example.com?query=whatever&page=1&order=date 

问题是,对订货的ASP代码,我不想来处理所有其他查询字符串。所以我需要一个函数来处理它,我和能够执行类似下面的例子:

<a href="?<%= add_querystring(qs, "order", "date") %>">date</a> 
<a href="?<%= set_querystring(qs, "page", page + 1) %>">next page</a> 
<a href="?<%= add_querystring(del_querystring(qs, "page"), "allpages") %>">all pages</a> 

这就是我要去,如果我仍然无法找到一个准备做的只是一个初步设想解决方案...再一次,只是想知道是否有什么新的东西来处理所有这些我还没有想到的方式。

回答

3

如果它是任何人的利益的,这里是比较混乱的代码,我昨天推出:

'Build a string QueryString from the array Request 
function bdl_qs (req_qs) 
    dim result, qa, item 
    result = empty 
    qa = "?" 
    if isnull(req_qs) or isempty(req_qs) then req_qs = Request.QueryString 
    for each item in req_qs 
     result = result & qa & item 
     result = result & "=" & req_qs(item) 
     qa = "&" 
    next 
    bdl_qs = result 
end function 

'Build a string QueryString ontop of the supplied one, adding the query and/or value(s) to it 
function add_qs (qs, q, s) 
    dim result 
    result = qs 
    if left(result, 1) = "?" then 
     result = result & "&" & q 
    else 
     result = "?" & q 
    end if 
    if not isnull(s) and not isempty(s) then 
     result = result & "=" & s 
    end if 
    add_qs = result 
end function 

'Build a string QueryString ontop of the supplied one, removing the selected query and/or values 
function del_qs (qs, q) 
    dim result, item 
    result = qs 
    if left(qs, 1) = "?" then 
     dim rqs, qa 
     rqs = result 
     result = "?" 
     rqs = right(rqs, len(rqs)-1) 'remove the "?" 
     rqs = Split(rqs, "&") 'separate the queries 
     qa = "" 
     for each item in rqs 
      dim rq 
      rq = Split(item, "=") 'separate the query to analyze the name only 
      if rq(0) <> q then 'good for rebuilding 
       result = result & qa & item 
       qa = "&" 
      end if 
     next 
    end if 
    del_qs = result 
end function 

'Build a string QueryString ontop of the supplied one, setting the query to the value 
function set_qs (qs, q, s) 
    set_qs = add_qs(del_qs(qs, q), q, s) 
end function