Solved

Using Query_String but removing values from the string...

Posted on 2004-10-29
173 Views
Last Modified: 2013-12-24
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
0
Question by:Ike23
    8 Comments
     
    LVL 5

    Expert Comment

    by: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
    0
     
    LVL 14

    Expert Comment

    by: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 :-)
    0
     
    LVL 4

    Author Comment

    by:Ike23
    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.
    0
     
    LVL 17

    Expert Comment

    by:Tacobell777
    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
    0
     
    LVL 4

    Author Comment

    by:Ike23
    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","&"),"&")>
    0
     
    LVL 14

    Expert Comment

    by:Renante Entera
    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 :-)
    0
     
    LVL 4

    Author Comment

    by:Ike23
    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
    0
     
    LVL 14

    Accepted Solution

    by:
    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","&"),"&")>
    </cfif>

    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.


    Regards!
    eNTRANCE2002 :-)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Article by: kevp75
    Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
    Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
    This video discusses moving either the default database or any database to a new volume.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now