ColdFusion - Nested Variable (dynamic variable read) [duplicate] - variables

This question already has answers here:
Coldfusion - variable field name when looping through database query results
(2 answers)
Closed 9 years ago.
Edit: The QUESTION is a duplicate, but the ANSWER is not!
I have the following code:
<cfquery name="contact" datasource="thesource">
SELECT * FROM #table# WHERE foo = '#bar#'
</cfquery>
and then later on (this is the problem part):
<cfloop from="0" to="9" index="i">
<cfset thisvar = Evaluate("contact.check" & i) />
<cfoutput>
#thisvar#
</cfoutput>
</cfloop>
Upon execution, it throws a nice big "Variable contact.check0 is Undefined". However, #contact.check0# will output just fine if hardcoded.
Any ideas on how to fix this?
Note:
I have seen Coldfusion - variable field name when looping through database query results, and although the problem seems to be exactly the same, the solutions do not work. As per the comments, I am also getting a "cannot be converted to a number" error. I notice Tomalak mentions "a little catch", but never says what it is. In the article he links to, I have tried every syntactically equivalent form, and it all throws an error...either cannot convert to a number or is not defined.
Also, I'm aware Evaluate() has overhead and "should not be used". I'll take any solution that works, whether it has Evaluate in it or not.
This is on ColdFusion 9.
Thank you
Edit: while a similar question already had an answer, this question had a different cause. See accepted answer below.

First of all, standard blub about escaping inputs. cfqueryparam exists for a reason. That aside you can do this without evaluate. Give this a try instead, including a check to make sure there is actually a result
<!--- make sure that there is actually a result --->
<cfif contact.RecordCount EQ 0>
NO RESULT
<cfelse>
<cfloop from="0" to="9" index="i">
<!--- assumes that you want row 1 --->
<!--- check.CurrentRow could also be used instead of 1 --->
<cfset thisvar = contact['check'&i][1] />
<cfoutput>
#thisvar#
</cfoutput>
</cfloop>
</cfif>
I suspect that it is attempting to Evaluate the column without specification of the row number, while when hard-coding it you'll find it uses the current row.
edit as Dan Bracuk pointed out this is an exact duplicate of the question you linked in your question, Coldfusion - variable field name when looping through database query results

This is ridiculous and absurd. Working code (moved things around a bit):
<cfoutput>
<cfloop from="0" to="9" index="i">
<cfset thisvar = contact["check"&i][1] />
#thisvar#
</cfloop>
</cfoutput>
NOT working code:
<cfoutput query="contact"> <!--- this line here --->
<cfloop from="0" to="9" index="i">
<cfset thisvar = contact["check"&i][1] />
#thisvar#
</cfloop>
</cfoutput>
Someone else had put cfoutput tags around the whole page. While this normally wouldn't be a problem, they ALSO declared the query attribute. This somehow overwrote the original query from the db. Calling contact.check0 worked just fine, but building the variable name dynamically, I think, was attempting to reference the cfoutput's query attribute.
Thanks to Peter Boughton for the suggestion to extract the problem code...which led me to this realization, and Simon for posting the syntactically correct code. Points go to them.

Related

Strange Behavior of ColdFusion Variable [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am utilizing Bootstrap tabs to display some data in my ColdFusion project. I am attempting to loop through a list of classes that are returned from the database and then getting information based on that class. Here is the snippet of code that applies to my question:
<div class="tab-content" id="class-tabs-content">
<cfset allClassIDs = ValueList(classes.class_id)>
<cfset numOfClasses = ListLen(allClassIDs)>
<cfloop from="1" to="#ListLen(allClassIDs)#" index="i">
<cfset myIndex = ListGetAt(allClassIDs, i)>
<cfoutput>
<div class="tab-pane fade<cfif i EQ 1> show active</cfif>" id="panel-#myIndex#" role="tabpanel" aria-labelledby="tab-#myIndex#">
<!--- The following returns a cfquery --->
<cfset items = CreateObject("component", "com.modules.reels").getInventoryByDivisionAndClass("query", division.location_id, myIndex)>
#items.RecordCount# <-- CODE FAILS ON THIS LINE WITH MESSAGE "variable [ITEMS] doesn't exist"
</div>
</cfoutput>
</cfloop>
</div>
UPDATE: I tried a simpler example of what I was trying to do, and I got the same error: variable [ITEMS] doesn't exist. Here is the simplified code block I tried:
<cfset classes = CreateObject("component", "com.modules.reels").getClasses("query")>
<cfset ClassIDs = ValueList(classes.class_id)>
<cfloop from="1" to="#ListLen(ClassIDs)#" index="i">
<cfset ClassID = ListGetAt(ClassIDs, i)>
<cfoutput>
<cfset items = CreateObject("component", "com.modules.reels").getInventoryByDivisionAndClass("query", 3, ClassID)>
<cfdump var = "#items#">
</cfoutput>
</cfloop>
I do not understand what could possibly be going on here! How can the variable not exist when I set it on the previous line and the cfset did not throw an error? Please help!
Thank you to #rrk for your comments. As it turns out (and I feel really stupid), getInventoryByDivisionAndClass() was missing its <cfreturn> statement!

Coldfusion set dynamic columns for query output [duplicate]

This question already has answers here:
Dynamic Variable Naming and Reference (ColdFusion)
(2 answers)
Getting complex object error when trying to output query values
(1 answer)
Closed 6 years ago.
I am trying to set dynamic query column headers to get their values from a query.
<cfoutput query="qryGetData">
<cfloop from="-18" to="18" index="i">
<cfif i GTE 0>
<cfset variables["target_MonthPlus_#abs(i)#"] = "Testing" />
<td>
<cfoutput>#variables["target_MonthPlus_#abs(i)#"]#</cfoutput>
</td>
<cfelse>
<cfset variables["target_MonthMinus_#abs(i)#"] = "Testing" />
<td>
<cfoutput>#variables["target_MonthMinus_#abs(i)#"]#</cfoutput>
</td>
</cfif>
</cfloop>
Code I have does not really work, I found it from another answer and I have tried all I can think of and tried using EVALUATE() even though I know I should not use.
So basically my query has 37 month fields starting from target_MonthMinus18 to target_MonthMinus1. And then target_MonthPlus0 to target_MonthPlus18. I have taken care of that plus and minus with the CFIF as you can see above. So only other thing different is that value of the month.
Closest things I have got to actually name the columns dynamically is something like this, but this just outputs the name of the column, which would return target_MonthPlus0, target_MonthPlus1, targetMonthPlus2, etc.
But I need to use that name to return the actual value of the columns from the query.
<cfif i GTE 0>
<cfset monthInLoop = "target_MonthPlus_" & #ABS(i)#>
<td>
<cfoutput>#monthInLoop#</cfoutput>
target_monthMinus18 is a column name that may return a value of 100 from qryGetData that I need to display in its td
target_monthMinus17 is a column name that may return a value of 95 from qryGetData that I need to display in its td
target_monthPlus17 is a column name that may return a value of 205 from qryGetData that I need to display in its td
and so on...We are always going back 18 months in the past and 18 months in future as you can tell.
I have found several questions similar to this and have applied to my code but somehow they are trying to do different things or I still do not understand what I am doing wrong.
Thanks in advance for your help :)
PC
I think you may be over thinking this. You can access your columns directly from within the query using something like this:
<cfloop query="qryGetData">
<cfloop from="-18" to="18" index="i">
<cfif i GTE 0>
<td>
<cfoutput>#qryGetData["target_MonthPlus_" & abs(i)][currentrow]#</cfoutput>
</td>
<cfelse>
<td>
<cfoutput>#qryGetData["target_MonthMinus_" & abs(i)][currentrow]#</cfoutput>
</td>
</cfif>
</cfloop>
</cfloop>
This takes advantage of CF's query array syntax. Keep in mind you also have qryGetData.columnlist to work with (a list of all your columns). You might be able to work with that creatively as well.

cfml variable inside cfoutput text

I'd like to resolve this problem. I have a variable:
<cfset myvar = "mytext <cfinclude template=""test.cfm"">">
and I have test.cfm with an amount of cfml code.
and I'm trying to make cfoutput like this:
<cfoutput>#myvar#</cfoutput>
I'd like that my page doesn't show this output:
mytext <cfinclude template="test.cfm">
but:
mytext followed by the execution of cfml code inside test.cfm
Is it possible?
I am not certain what you are trying to do from your question but if my guess is correct this may do what you want:
<cfsavecontent variable="myvar"><cfoutput>
mytext
<cfinclude template="test.cfm">
</cfoutput></cfsavecontent>
<cfoutput>
#myvar#
</cfoutput>
Note: You may not need the cfoutput inside the cfcontent tag, it depends on your page setup so I have just added it.
I have no idea what your included test.cfm file is doing since you did not share that information but this might work for you:
<cfset myvar = "mytext ">
<cfoutput>#myvar#</cfoutput>
<cfinclude template="test.cfm">
Assuming that the included file generates the output that you are looking for.
In your code, you are trying to set the cfinclude tag itself to another variable. This will never work. However, I have tested the following on a live server and it does work.
<cfsavecontent variable="inc">
<cfinclude template="test.cfm">
</cfsavecontent>
<cfoutput>mytext #inc#</cfoutput>
In this example, everything between the cfsavecontent tags is saved to a variable, in this case inc. Then you are able to reference that variable in the cfoutput tag.
Or, to use <cfset> as in your code:
<cfsavecontent variable="inc">
<cfinclude template="test.cfm">
</cfsavecontent>
<cfset myvar = "mytext" & inc>
& is the CFML string concatenation operator.
It's difficult to know for sure without seeing more of your code, but if you are trying to use includes in this way your code is perhaps disorganized. Providing a full example would help provide some context.

ColdFusion Query Output Displaying Variable Name Instead Of Field Value

I'm an "old dog" and largely self-taught on this stuff and can usually make things work (primative and convoluted as it might be), but this is the first thing that has me really stymied.
I didn't want to burden everyone with a lot of stuff to try to explain, but, here is perhaps a better explanation and example:
(#Leigh - and thank you for your time and help!) - The query is dynamic because what I desire to have is a single "universal" page combination (form page plus accompanying action page) that is used to edit multiple different (but fairly similar) record sets (so that I don't have to write a whole bunch of individual form/action page pairs).
When this "universal" "change" form page is invoked, it is passed the "ID" variable for the particular record to be edited, along with a "listID" variable unique to the particular record set containing the record to be edited.
Using the "URL.listID", the form page then looks at a pre-defined included list of record set variables (datasource, query table, field for column 1, field for column 2, etc.) pertaining to the value of the "listID" and sets (using ) the dynamic variables. Example - if "listID" is "5", which has only one column:
<cfif #URL.listID# EQ 5>
<cfset page_title = 'Change Member Role Picklist'>
<cfset datasource = '#Session.db_docs#'>
<cfset query_tbl = 'tblMemberRole'>
<cfset columns = 1>
<cfset column1_label = 'Member Role'>
<cfset column1_field = "role">
<cfset column1_input_type = "text">
<cfset column1_input_size = "100">
<cfset column1_input_maxlength = "100">
</cfif>
The query uses those variables ("ID" plus the others it got from the above list) to retrieve the individual record to be edited, and populate the "change" form.
Run query:
<cfquery name="cfqGetItem" datasource="#datasource#">
SELECT *
FROM #query_tbl#
WHERE ID = <cfqueryparam value="#URL.ID#" cfsqltype="cf_sql_integer">
</cfquery>
My "change" form (abbreviated here without table HTML) to be populated would be:
<form name="form_item_chg" action="chg_item2.cfm" method="post" enctype="multipart/form-data">
<input type="text" name="#column1_field#" maxlength="#column1_input_maxlength#" size="#column1_input_size#" value="#cfqGetItem[column1_field][currentRow]#">
<input type="Submit" value="Post Changes">
</form>
However, instead of the "change" form being populated with the VALUE for field "role", it instead is trying to use the variable name "column1_field", which it says is (true, of course) undefined in the query.
When I tried "#cfqGetItem[column1_field][currentRow]#", it says "Variable currentrow is not defined".
When I tried "#cfqGetItem.column1_field#", it says "Element column1_field is not defined in query cfqGetItem".
I apologize in advance for not knowing/using all the correct terminology, and hope I am explaining this reasonably clearly. I suspect I will have to revert to writing individual form-page/action-page pairs. Thank you to all for your time and help!
ORIGINAL POST:
I'm not highly technical, and this is probably something simple, but here is
my dilemma, where I am attempting to retrieve a single record using a variable name in the query.
First, I define some variables:
<cfset ID = #Form.ID#<!--- the single record I want to retrieve, passed from a form --->
<cfset datasource = 'MyDatabase'>
<cfset query_tbl = 'MyDatabaseTable'>
<cfset field1 = 'actual_fieldname1'><!--- field in MyDatabaseTable --->
<cfset field2 = 'actual_fieldname2'><!--- field in MyDatabaseTable --->
ETC.
Then, to retrieve this single record, I run a query using those variables:
<cfquery name="cfqGetItem" datasource="#datasource#">
SELECT *
FROM #query_tbl#
WHERE ID = #ID#
</cfquery>
Then, I attempt to display the query output:
EITHER AS
<cfoutput>
<p>#cfqGetItem.field1#
<p>#cfqGetItem.field2#
</cfoutput>
OR, AS
<cfoutput>
<p>#cfqGetItem[field1][currentRow]#
<p>#cfqGetItem[field2][currentRow]#
</cfoutput>
In each case, I get a similar CF error message: "Element field1 is not defined in query cfqGetItem", or "Variable currentrow is not defined".
How can I get the query output to generate the actual values for the record instead of the variable names?
Thank you very much for any help!
If you change this:
<cfoutput>
<p>#cfqGetItem[field1][currentRow]#
<p>#cfqGetItem[field2][currentRow]#
</cfoutput>
to this:
<cfoutput>
<cfloop query="cfqGetItem">
<p>#cfqGetItem[field1][currentRow]#
<p>#cfqGetItem[field2][currentRow]#
</cfloop>
</cfoutput>
You should be ok. However, this is the simplest way
<cfoutput query="cfqGetItem">
<p>#actual_fieldname1#
<p>#actual_fieldname1#
</cfoutput>
Setting the table and field names to variables complicates matters. Unless they really can vary, don't do it. Also, the cfqueryparam tag is your friend. Use it in places like this:
where id = <cfqueryparam
cfsqltype = "cf_sql_integer"
value = "#id#">
You can try out like this also:
<cfquery
name="GetParks" datasource="cfdocexamples"
>
SELECT PARKNAME, REGION, STATE
FROM Parks
ORDER BY ParkName, State
</cfquery>
<cfoutput>
<p>#GetParks.PARKNAME#
<p>#GetParks.REGION#
</cfoutput>
<cfoutput >
#GetParks['state'][GetParks.RecordCount]#
</cfoutput>
<cfoutput >
#GetParks['state'][2]#
</cfoutput>

Access resultset value of cfprocresult name in cfif

I am new to ColdFusion and want to rewrite my project from ASP.NET to learn ColdFusion step by step.
My SQL Server stored procedure for authentication (ASP.NET) looks like this:
CREATE PROCEDURE sp_auth
#suser VARCHAR(20),
#spswd VARCHAR(20)
AS
SELECT COUNT(*) FROM Users.SUsers WHERE suname=#suser AND supasswd=#spswd AND off_bit=0
GO
Application.cfc snippet.
<cfstoredproc procedure="sp_auth" datasource="cftraderelay">
<cfprocparam value="#cflogin.name#" cfsqltype="CF_SQL_VARCHAR" maxlength="20" type="in">
<cfprocparam value="#cflogin.password#" cfsqltype="CF_SQL_VARCHAR" maxlength="20" type="in">
<cfprocresult name="auth_pass" maxrows="1">
</cfstoredproc>
<cfif auth_pass GTE 1>
<cfloginuser name="#cflogin.name#" password = "#cflogin.password#" roles="#roles#">
<cfelse>
<cfoutput>
<h2>Your login information is not valid.<br> Please Try again</h2>
</cfoutput>
<cfinclude template="login.cfm">
<cfabort>
</cfif>
What is right form of 'cfif auth_pass GTE 1' logic? How to access the value of 'auth_pass'.
Cos I get "Complex object types cannot be converted to simple values" error by far.
Am I strictly supposed to use out type of cfprocparam in this case, not cfprocresult (modify SQL query for the OUTPUT variable)?
Can I access cfprocresult value right in the cfif statement?
auth_pass contains a recordset, not a number, so you cannot compare it to 1. You perhaps want auth_pass.recordCount?
The best tool for examining the contents of a variable is <cfdump>. That and familiairising yourself with the docs for the code you're using, eg: <cfprocresult>, <cfstoredproc>, <cfquery> (the latter for details on query objects) if you run into problems and can't guess the solution.