Link to home
Start Free TrialLog in
Avatar of Rush_2112
Rush_2112

asked on

cfselect bind to another cfselect

Hi all, need help with a cfselect bind.

I'm using this as a tutorial: http://tutorial544.easycfm.com/

Here's my page:  http://pcgrecords.com/vol/test.cfm

The first select, year, isn't bound, so ignore that.  But the rest of the cfselects on that page have a bind to the one before it.  (So the 'makes' one populates the 'models', and etc.)

Everything is ok until I get to the last one.  What I really want, is for the last cfselect, Select an OE, to be bound not just to the engine cfselect before it, but to the select model field as well.

Please help!

Here's the relevant code on the page, and the cfc is attached in a txt file.

   <tr>
         <td>Select Model:</td>
         <td><cfselect name="model" bindOnLoad="Yes" bind="cfc:data.getmodel({makes})" />
         </td>
</tr>
 <tr>
         <td>Select Engine:</td>
         <td><cfselect name="engine" bindOnLoad="Yes" bind="cfc:data.getengine({model})" />
         </td>
</tr>
<tr>
         <td>Select OE:</td>
         <td><cfselect name="OE"  bind="cfc:data.getOE({engine})" />
         </td>
</tr>

---------
ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America 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
Avatar of Rush_2112
Rush_2112

ASKER

Sorry, guess it didn't upload... its there now.   Thanks for helping.  

I was following a tutorial so I don't know what that line does or why it's duplicated.  Does the cfc help?
cfc.txt
Thanks, even without the rest of the code, you got it!
Looks like it's just duplicated in the data ie db table. So just add a DISTINCT in the query. That'll take care of it.

Btw, the tutorial code is a little outdated.  As of CF8.0.1 you can bind directly to a query, so there's no need to build a separate array.  Just return the query from the function.  Then specify the "value" and "display" column in your cfselect.  

It's also a good practice to 1) always VAR scope variables (prevent hard to trace threading problems) and 2) use cfqueryparam (sql injection) and 3) scope all variables

Here's how I'd rewrite it.  Unfortunately I can't test it out right now, watch out for typos.

FORM

<tr>
         <td>Select Model:</td>
         <td><cfselect name="model" bindOnLoad="Yes" bind="cfc:data.getmodel({makes})"
                 display="model" value="model" />
         </td>
</tr>
 <tr>
         <td>Select Engine:</td>
         <td><cfselect name="engine" bindOnLoad="Yes" bind="cfc:data.getengine({model})"
                 display="engine" value="engine" />
         </td>
</tr>
<tr>
         <td>Select OE:</td>
         <td><cfselect name="OE"  bind="cfc:data.getOE({model}, {engine})" 
                 display="OE" value="OE" />
         </td>
</tr>

Open in new window


CFC
<cfcomponent output="false">
    <cfset variables.dsn = "Volusion">

    <cffunction name="getmakes" access="remote" returntype="query">
        <cfset var getData = "">

        <cfquery name="getData" datasource="#variables.dsn#"> 
            SELECT DISTINCT make FROM ECM
            ORDER BY make
        </cfquery>

        <cfreturn getData />
    </cffunction>
    
    <cffunction name="getmodel" access="remote" returntype="query">
        <cfargument name="make" type="string" required="true">
    
        <cfset var getData = "">
        <cfquery name="getData" datasource="#variables.dsn#">
            SELECT DISTINCT model FROM ECM
            WHERE make = <cfqueryparam value="#arguments.make#" cfsqltype="cf_sql_varchar">
            ORDER BY model
        </cfquery>

        <cfreturn getData />
    </cffunction>
    
    <cffunction name="getengine" access="remote" returntype="query">
        <cfargument name="model" type="string" required="true">
    
        <cfset var getData = "">
        <cfquery name="getData" datasource="#variables.dsn#">
            SELECT  engine FROM ECM
            WHERE model = <cfqueryparam value="#arguments.model#" cfsqltype="cf_sql_varchar">
            ORDER BY model
        </cfquery>

        <cfreturn getData />
    </cffunction>
    
    <cffunction name="getOE" access="remote" returntype="query">
        <cfargument name="model" type="string" required="true">
        <cfargument name="engine" type="string" required="true">
    
        <cfset var getData = "">
        <cfquery name="getData" datasource="#variables.dsn#">
            SELECT OE FROM ECM
            WHERE model = <cfqueryparam value="#arguments.model#" cfsqltype="cf_sql_varchar">
            AND   engine = <cfqueryparam value="#arguments.engine#" cfsqltype="cf_sql_varchar">
            ORDER BY OE
        </cfquery>

        <cfreturn getData />
    </cffunction>
    
</cfcomponent>

Open in new window

> Thanks, even without the rest of the code, you got it!

Oops.. sorry I missed your last comment before posting :) (But having a fuller example for the archives couldn't hurt) Anyway, glad I could help.
thanks for the update!  I'll try that out too!