I am new to ColdFusion and trying to use cfloop for the below code:
<cfscript>
var origRate = 0;
var toRate = 0;
rates = myQuery.filter(function (obj) {
return (obj.code == arguments.origCode || obj.code ==
arguments.toCode)
})
</cfscript>
I modified below, the original code and inserted the above new code to avoid the inline sql queries:
<cfquery name="rates" dbtype="query">
select code, rate
from myQuery
where code = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.origCode#" />
or code = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.toCode#" />
</cfquery>
I tried using cfloop without changing to the previous code as below but it is not working:
<cfloop query="rates">
<cfscript>
if (code == arguments.origCode) origRate = rate;
if (code == arguments.toCode) toRate = rate;
</cfscript>
</cfloop>
Once the second block of code was inserted by commenting out the first block of code above, it did not load the page. If anyone has an idea, I really appreciate it. Thank you in advance!
There were some missing details about the application and data, so I made a couple of assumptions. It appears that you have a query object that you want to filter and pull rates from for an
origCode
and atoCode
. Without knowing more about your data structure and what you plan to do with it, I can only make some general suggestions. I still maintain that it would be much better to filter in the query, but I understand the limitation. Since you have to filter inside your application, both the bulk of the base data you initially return and the processing to filter those records will negatively impact the performance.First thing I did was to set up a fake query object. This is where my first assumption comes into play. I assumed that your
code
won't be any duplicated in your table, and that the code will have arate
associated with it.I would not recommend a Query of Query here, because it's a lot of overhead for something that can be accomplished fairly easily.
I created a function that you can pass in your
origCode
and thetoCode
, and it will return you a structure of theorigRate
and thetoRate
. I included some comments in the code, so you will be able to see what I was doing. The bulk of the function is using thefilter()
closure to filter the query records down. If you are able to filter through SQL, you'll be able to eliminate this block.To assign the
origRate
andtoRate
, we first create aratesStruct
return value to hold the structure of the rates. After we filter our query, we just loop through those filtered results and check to see if thecode
in the row matches with our input variables. Another one of my assumptions was that the database would return no more than two records (oneorigCode
and onetoCode
, or neither). If it is possible to return more than one row for acode
, then the output codes will be overwritten by the last related row in the query. If there are other rows appropriate for sorting, then they can be used and only select the top row for the needed rate. I also defaulted the returned rates to a -1 to signify that norate
was found for thecode
. That can be changed if needed.After that, I just ran a few tests to make sure we didn't get any wonkiness. Code is at https://trycf.com/gist/c3b87ca7c508562fd36f3ba6c73829c7/acf2016?theme=monokai.
And again, I think this can all probably be done within the database itself. Probably by giving you access to a stored procedure that you can pass
origCode
andtoCode
to.