2010-08-12 37 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