Link to home
Start Free TrialLog in
Avatar of Ike23
Ike23Flag for United States of America

asked on

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!

Tim
Avatar of kkhipple
kkhipple

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..


KDK
Avatar of Renante Entera
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#
</cfquery>

<!--- 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. --->
<cfoutput>
  <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>
</cfoutput>

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.


Goodluck!
eNTRANCE2002 :-)
Avatar of Ike23

ASKER

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.  

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

Or

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

I do believe you are making it hard for yourself
Avatar of Ike23

ASKER

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","&"),"&")>
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#">
    <cfelse>
      <cfset NewQS = thisParam>
    </cfif>
  </cfif>
</cfloop>

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

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

Hope this helps you.  Just try it.


Regards!
eNTRANCE2002 :-)
Avatar of Ike23

ASKER

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!

Tim
ASKER CERTIFIED SOLUTION
Avatar of Renante Entera
Renante Entera
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial