Search code examples
sqlcoldfusiondistinctcfquery

Filtering cfquery results


I am editing already existing code, which is why this question is formed as it is. I am attempting to use a query that already exists and without adding more form variables through the url.

So my problem: I have a query that is being run, and this query is being used to populate two drop down lists on the page. One list is the state of operation, and the other is the actual site of operation. At the start of the page, I want the pull down to have only distinct results from the query, currently it is displaying 120+ 'AL' 80+ 'MN' etc. Additionally I want second pull down to only have results that have the particular state as their state.

I know I can do this by adding more querys, and with a form variable, but I am wondering if there is a way to just filter results in a cfquery.


Solution

  • Here's what I'd do. Only show the state drop down first.

    This would be easier with some example code, but here's an example. Haven't tried it at all, but should be an okay start. Never remember how to set selects back to nothing selected...

    First Drop Down

    <cfoutput group= "state">
    <option value= "#state#">#state#</option>
    </cfoutput>
    

    Second Drop Down(s)

    <cfoutput group= "state" style= "display: none;">
    <select id= "#site#" class= "site">
    <option>Select a Site</option>
    <cfoutput>
    <option value= "#site#">#site#</option>
    </cfoutput>
    </select>
    </cfoutput>
    

    Add some JavaScript...

    $( "#state" ).change( function() {
    
    $( ".site" ).each( function() {
    $( this ).selected( '' ).hide();
    
    } );
    
    $( "select[id=" + $( this ).val() + "]" ).show();
    
    
    } );