Using Query_String but removing values from the string...

I want to allow my users to filter my table by each column by clicking on the header.  I am displaying the data from a query and then outputing it dynamically.  I want to pass the table name to sort by in the URL and also pass any other URL variables that exist in the QUERY_STRING CGI variable.  

The problem is that if I just use this: href="page.cfm?#QUERY_STRING#&orderby=fieldname" the next time a person clicks on a header from the same page their will be duplicate "orderby" values and it will mess up my query.  Is there an easy way to trim out the orderby value from the QUERY_STRING?  I figured that someone probably has run into this before, thanks!

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

well what i do when im working with sorting data is to do the following

<A HREF="page.cfm?tblName=#URL.tblName#&param=#URL.param#&orderby=#fieldname#">wahtever</A>

my opinion is that you probably  have structured the wrong way... i think what would be best Tim is that you paste some code and let us see what yo are exactly doing..

Renante EnteraSenior PHP DeveloperCommented:
Hello Ike23!

You can simply do something like this :

<cfparam name="url.tblName" default="Table1">
<cfparam name="url.orderyby default="Column1">

<cfquery name="GetRecord" datasource="">
SELECT * FROM #url.tblName#
ORDER BY #url.orderyby#

<!--- If you want to change the sorting of the same table --->
<cfset Header1= "?tblName=#url.Table1#&orderyby=Column1">
<cfset Header2= "?tblName=#url.Table1#&orderyby=Column2">
<cfset Header3= "?tblName=#url.Table1#&orderyby=Column3">

<!--- If you want to pass a certain table with specific sort order --->
<cfset Header4= "?tblName=Table1#&orderyby=Column1">
<cfset Header5= "?tblName=Table2#&orderyby=Column2">
<cfset Header6= "?tblName=Table3#&orderyby=Column3">

<!--- For the links and just assume this is within the column of a table. --->
  <a href="page.cfm#Header1#">Header1</a>
  <a href="page.cfm#Header2#">Header2</a>
  <a href="page.cfm#Header3#">Header3</a>
  <a href="page.cfm#Header4#">Header4</a>
  <a href="page.cfm#Header5#">Header5</a>
  <a href="page.cfm#Header6#">Header6</a>

Actually, if you are on the same page.  You may not include the filename on the "attribute href value".  You can simpy call <a href="#Header1#">Header1</a>.

Hope this helps you.  Just try it.

eNTRANCE2002 :-)
Ike23Author Commented:
I have a bunch of different URL variables that don't relate to my query but that are needed for other aspects of the page.  I just want to filter out the orderby=whatever URL variables from the QUERY_STRING variable so that I can dynamically use this code all over my site when I want to pass all URL values except change one.  

Can I use the Replace() function somehow?  To be clear I want all the query_string to be passed everytime the header link is clicked so page.cfm?#Query_StringSpecial#&orderby=name.  I'm thinking that I can do a check before like

<cfif isdefined("QUERY_STRING")>
     <cfset QUERY_STRINGSPECIAL = Replace(QUERY_STRING,'orderby','','all') but I can't get it to work right.  

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

<cfset QUERY_STRINGSPECIAL = replaceNoCase(QUERY_STRING,'orderby','','all')/>


<cfset QUERY_STRINGSPECIAL = reReplaceNoCase(QUERY_STRING, "&orderby=[^&]", "", "all")/>

I do believe you are making it hard for yourself
Ike23Author Commented:
I need to delete the entire name=value pair form the query string.  If you know of an easier way than I welcome the suggestion!  Here's what I ended up using.

<cfset newQS = ListDeleteAt(CGI.QUERY_STRING, ListContainsNoCase(CGI.QUERY_STRING, "orderby","&"),"&")>
Renante EnteraSenior PHP DeveloperCommented:
Hi Ike23!

Try to evaluate this code, perhaps this is what you need :

<cfparam name="NewQS" default="">

<cfset QSLength = ListLen(cgi.QUERY_STRING,"&")>

<cfloop index="i" from="1" to="#QSLength#">
  <cfset thisParam =  ListGetAt(cgi.QUERY_STRING,i,"&")>
  <cfif FindNoCase('orderby',thisParam,1) eq 0>
    <cfif NewQS neq ''>
      <cfset NewQS = "#NewQS#&#thisParam#">
      <cfset NewQS = thisParam>

<cfif NewQS neq ''>  
  <cfset Header1 = "#NewQS#&orderby=column1">
  <cfset Header2 = "#NewQS#&orderby=column2">
  <cfset Header3 = "#NewQS#&orderby=column3">
  <cfset Header1 = "orderby=column1">
  <cfset Header2 = "orderby=column2">
  <cfset Header3 = "orderby=column3">

<a href="page.cfm?#Header1#">Header1</a><br>
<a href="page.cfm?#Header2#">Header2</a><br>
<a href="page.cfm?#Header3#">Header3</a><br>

Hope this helps you.  Just try it.

eNTRANCE2002 :-)
Ike23Author Commented:
This works and is a lot less code:
<cfset newQS = ListDeleteAt(CGI.QUERY_STRING, ListContainsNoCase(CGI.QUERY_STRING, "orderby","&"),"&")>

Then I can pass all the required variables that run my page without having to type them all into the url string.  This will allow me to add an order by filter along with every other variable that was initially passed to the page since it can vary from client, user etc.  Thanks for trying though, I appreciate it.  You should try this code, it works pretty well and is only one line!

Renante EnteraSenior PHP DeveloperCommented:
Yah! you're right Ike23...

I apologize for isolating your code.  Actually, it's really less code and simple.  I haven't think of that :-).

Anyway, I just only have one suggestion.  Apply also checking of the value for "CGI.QUERY_STRING", if there are cases that you use that code in which there's is no "URL" parameters being pass upon first browsing on a specific page.

For example : If a certain page that most parameters have been set to default values.  Have your checking like this :

<cfif ListContainsNoCase(cgi.query_string,"=","&") neq 0>
  <cfset newQS = ListDeleteAt(CGI.QUERY_STRING,ListContainsNoCase(CGI.QUERY_STRING, "orderby","&"),"&")>

Or if there are cases in which user attempts to type in the address bar something like : page.cfm?test.  It is expected that you will encounter an "Invalid list index 0" error in funtion ListDeleteAt.

Hope this helps you.

eNTRANCE2002 :-)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.