Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arrays in Cold Fusion

Status
Not open for further replies.

Guest_imported

New member
Joined
Jan 1, 1970
Messages
0
I am new to Cold Fusion and trying to learn how to work with arrays.

Please advise what I am doing wrong with this array.

[tt]

<html>
<head>
<title>Learning Arrays in Cold Fusion</title>
</head>

<body>
<cfquery name=&quot;myq&quot; datasource=&quot;database&quot;>
select firstname,department_id,salary
from Employees
</cfquery>

<cfoutput query=&quot;myq&quot;>
<cfset firstname = myq.ArrayNew(1)>
<cfset firstname[1] = #myq.firstname#>
<cfset firstname[2] = #myq.firstname#>
<cfset firstname[3] = #myq.firstname#>
<cfoutput>#myq.firstname[2]</cfoutput>

</body>
</html>
[/tt]
 
You are creating your array inside the cfoutput, so it is getting overwritten. Set it right before your <cfoutput query=&quot;myq&quot;> and see if that fixes your problem.

Also, you have another <cfoutput> inside the first <cfoutput> which will cause a nesting error.

try

<cfset firstname = myq.ArrayNew(1)>
<cfoutput query=&quot;myq&quot;>
<cfset firstname[1] = #myq.firstname#>
<cfset firstname[2] = #myq.firstname#>
<cfset firstname[3] = #myq.firstname#>
</cfoutput>

<cfoutput>#myq.firstname[2]</cfoutput>
 
First, why are you creating the array in the &quot;myq&quot; namespace?. Why not just:

<cfset firstname = ArrayNew(1)>

Second, why are you assigning myq.firstname to three elements of the array for each record in the query? Are you expecting only ONE record to be returned by the query? When the loop has completed,

Also, get rid of the extra pound signs. They are unnecessary and make the code harder to read. Generally, when referencing variables, you do not need pound signs inside the <cfset> tag or inside ColdFusion functions, except when the variable is also between quotes (as in IsDefined() or TO=&quot;#ArrayLen(myarray)#&quot;.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top