Copy link to clipboard
Copied
I have a page with URL Parameters posted to it.
Is there a way to to loop through the URL parameters and display the name and value of all the URL Parameters?
Thanks!
Copy link to clipboard
Copied
I got this so far:
<cfloop item="key" collection="#URL#">
<cfoutput>#key# - #URL[key]#<br></cfoutput>
</cfloop>
What I need to do is Sort the Names in ascending order ignoring case.
And I also need to remove from this list the name + value of one of the URL Parameters I know exists in the querystring
Copy link to clipboard
Copied
One idea off the top of my head.
<cfset keyList = listSort(structKeyList(url),'textnocase','asc')>
<!--- check my syntax on the listSort() function --->
<cfloop list="#keyList#" index="key">
<cfoutput>#key# - #url[key]#</cfoutput>
</cfloop>
Copy link to clipboard
Copied
That's perfect.
Now is there a way to remove a particular URL Parameter from the list by name?
Copy link to clipboard
Copied
You could modify the URL structure with structure functions such as structDelete(). Be aware there is some funky behaviour with this option dealing with the readOnly nature of the Form and Url structures. But it would probably work.
You could modify the KeyList with list functions such as ListFind() and ListDeleteAt().
You could modify the KeyList with string functions such as find() and such.
Copy link to clipboard
Copied
Also, why does looping over a list convert the URL Parameter name to all caps.
i.e. "id" becomes "ID". This is a problem in my application.
Is there a way to get it to return the URL Parameter NAMES in a case-sensitive fashion
Also @Dan Query_String works but I need to remove the ampersands and one particular URL Parameter in the request.
I'll look at the list functions
Copy link to clipboard
Copied
Basically I need to convert this URL QUERYSTRING
paramOne=345¶mTwo=346¶mThree=347
to
paramOne345paramThree347
with
1. paramTwo removed from the list
2. no & ampersands
3. URL names should remain as is respecting case.
4. Parameter names ordered ascendingly
YIKES!
Copy link to clipboard
Copied
<cfstet foobar = structNew()>
<cfloop list="#cgi.querystring#" index="elem" delimiters="&">
<cfif listFirst(elem,"=") NEQ "paramTwo")>
<cfset foobar[listFirst(elem,"=")] = listLast(elem,"=")>
</cfif>
</cfloop>
<cfdump var="#foobar#">
A structure may not be the ideal data type to use for your requirements, but it is a quick and simple way to demonstrate how to loop over a list with any delimiter.
Copy link to clipboard
Copied
@Ian, I got an empty structure from cfdump. not sure why.
@dan I think I found my solution however inefficient it appears
<cfset keyList = URLDecode(CGI.QUERY_STRING)>
<cfset keyList = listSort(keyList,'textnocase','asc','&')>
<cfset paramTwo = Refind("paramTwo", keyList)>
<cfset keyList = removechars(keylist, paramTwo, 38)>
<cfset keyList = ReReplace(keyList, "&","","ALL") >
<cfset keyList = ReReplace(keyList, "=","","ALL") >
<cfoutput>#keyList#</cfoutput>
This only works because I know how many characters will always be the value of paramTwo. So I found the position of the paramTwo and added the amount of characters afterward to remove.
If the number of characters in the value of that parameter changes in the future I guess I'm screwe
Copy link to clipboard
Copied
Here is another way to get rid of param2.
<cfset newstring = "">
<cfloop list = "#cgi.query_string#" delimiters="&" index = "element">
<cfif element contains "param2" is false>
<cfset newstring = newstring & replace(element, "=", "")>
closing tags
Copy link to clipboard
Copied
@Dan that code seems nearly perfect, much cleaner than mine.
But I cant figure where in the block I could sort the params in ascending order before output?
Copy link to clipboard
Copied
<cfset keyList = URLDecode(CGI.QUERY_STRING)>
<cfset keyList = listSort(keyList,'textnocase','asc','&')>
<cfset paramTwo = Refind("paramTwo", keyList)>
<cfset keyList = removechars(keylist, paramTwo, 38)>
<cfset keyList = ReReplace(keyList, "&","","ALL") >
<cfset keyList = ReReplace(keyList, "=","","ALL") >
<cfoutput>#keyList#</cfoutput>
Firstly, why are you using reFind() and reReplace() when you're not using regular expressions? find() and replace() are sufficient for what you're doing here.
Is the above code doing what you want it to? it seems to me that if you had a query string which was this for example:
param3=drei¶m2=deux¶m1=one
You'd end up with this:
param1oneparam2deuxparam3drei
I guess I don't know what you're setting out to achieve, but that doesn't seem a very useful result to me.
If I was you, I'd be perhaps looping over your sorted list, and then treat each sub item (which would be like param1=one, using my example) as a list delimited by "=". Then you could check listFirst(theItem, "=") to see if its paramTwo, in which case skip it, otherwise [do whatever else you need to do].
--
Adam
Copy link to clipboard
Copied
<cfset keyList = URLDecode(CGI.QUERY_STRING)>
<cfset keyList = listSort(keyList,'textnocase','asc','&')>
....If I was you, I'd be perhaps looping over your sorted list, and then treat each sub item (which would be like param1=one, using my example) as a list delimited by "=". Then you could check listFirst(theItem, "=") to see if its paramTwo, in which case skip it, otherwise [do whatever else you need to do].
--
Adam
@lovewebdev,
That approach sounds like it would work, given your requirements. Did you try it?
Copy link to clipboard
Copied
YES!! Almost there...
I cant figure out though why its outputting the string twice??
<cfset newstring = listSort(cgi.query_string,'textnocase','asc', '&')>
<cfloop list = "#newstring#" delimiters="&" index = "element" >
<cfif element contains "param2" is false>
<cfset newstring = newstring & replace(element, "=", "")>
</cfif>
</cfloop>
<cfoutput>#URLDECODE(newstring)#</cfoutput>
Copy link to clipboard
Copied
changed the names of variables around in the first couple of lines to:
<cfset newstring ="">
<cfset newstring1 = URLDECODE(listSort(cgi.query_string,'textnocase','asc', '&'))>
<cfloop list = "#newstring1#" delimiters="&" index = "element" >
.....
<cfoutput>#newstring#</cfoutput>
and it worked.
Thanks guys!!!!!!!!!!
Copy link to clipboard
Copied
I cant figure out though why its outputting the string twice??
It is not. Look at your code. See how you are intializing the "newstring" variable with the query string. Then appending more information onto that same variable ...
<cfif element contains "param2" is false>
Be careful using "contains". Contains searches for a match _anywhere_ in the string. So contains "param2" would match the string "param222", "param21", etcetera.
Copy link to clipboard
Copied
Be careful using "contains". Contains searches for a match _anywhere_ in the string. So contains "param2" would match the string "param222", "param21", etcetera.
Thanks for that warning.
I do know the name of the parameter I need to remove. Can i rewrite this differently to be more accurate:
<cfif element contains "param2" is false>
I'm assuming you can't since the value of the URL Parameter is also in the element.
Copy link to clipboard
Copied
Yes. As Adam suggested, treat "element" as a list
... then treat each sub item (which would be like param1=one, using my example) as a list delimited by "=". Then you could check listFirst(theItem, "=") to see if its paramTwo, in which case skip it, otherwise [do whatever else you need to do].
Copy link to clipboard
Copied
The only part of that which looks difficult is maintaining the exact case of the parameter names. My solution would be to create a session variable with the names (in a list) exactly as you want them, and in the order you want them. Then, in your links, create your anchor tags with the parameters in the same order as they are in that list.
As you read up on the list functions, pay special attention to the choice of available delimiters.
Finally, take a peek at the replace function.
Copy link to clipboard
Copied
Look at cgi.query_string. Google "coldfusion listfunctions" if you don't know what to do with that variable.