Highlighted

Not getting all results in Array Populated from Query

Community Beginner ,
Jul 25, 2014

Copy link to clipboard

Copied

When running this it is only giving me the last value of my query in the array. Any thoughts on why this is happening? I appreciate any assistance. Thank you

<cfset result = arrayNew(1) />

           <cfset ArrayMedSchools = structNew() />

         

        <cfloop query="GetMedSchool" >

             <cfoutput>

              <cfset ArrayMedSchools.name = Institution />

              <cfset ArrayMedSchools.id = InstitutionID />

              <cfset arrayAppend(result,ArrayMedSChools) />

             </cfoutput>

        </cfloop>

        <cfset ArrayMedSchools = structNew()/>

       

        <cfdump var="#GetMedSchool#" label="Original Query">

        <cfdump var="#result#" label="resulting array">

I figured it out.

I had the   <cfset ArrayMedSchools = structNew()/> outside the loop instead of inside the loop.

Views

235

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Not getting all results in Array Populated from Query

Community Beginner ,
Jul 25, 2014

Copy link to clipboard

Copied

When running this it is only giving me the last value of my query in the array. Any thoughts on why this is happening? I appreciate any assistance. Thank you

<cfset result = arrayNew(1) />

           <cfset ArrayMedSchools = structNew() />

         

        <cfloop query="GetMedSchool" >

             <cfoutput>

              <cfset ArrayMedSchools.name = Institution />

              <cfset ArrayMedSchools.id = InstitutionID />

              <cfset arrayAppend(result,ArrayMedSChools) />

             </cfoutput>

        </cfloop>

        <cfset ArrayMedSchools = structNew()/>

       

        <cfdump var="#GetMedSchool#" label="Original Query">

        <cfdump var="#result#" label="resulting array">

I figured it out.

I had the   <cfset ArrayMedSchools = structNew()/> outside the loop instead of inside the loop.

Views

236

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 25, 2014 0
Engaged ,
Jul 25, 2014

Copy link to clipboard

Copied

So your query has X rows.  Does your array have the values for the last row in your query, duplicated X times?  Or does it just have one element, with the values from the last row in the query?

One thing you might want to try is resetting the structure within your query loop:

<cfset result = arrayNew(1)>

<cfset ArrayMedSchools = structNew()>

<cfloop query="GetMedSchool">

    <cfset ArrayMedSchools = structNew()>

    <cfset ArrayMedSchools.name = GetMedSchool.Institution>

    <cfset ArrayMedSchools.id = GetMedSchool.InstitutionID>

    <cfset arrayAppend(result, ArrayMedSchools)>

</cfloop>

<cfdump var="#GetMedSchool#" label="Original Query">

<cfdump var="#result#" label="resulting array">

Also note that you don't need to use <cfoutput> within your loop there.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 25, 2014 1
Engaged ,
Jul 25, 2014

Copy link to clipboard

Copied

Here you go... this is how you would want to do it.

<cfset result = [] />

<cfloop query="GetMedSchool" >

<cfset result[ArrayLen(result)+1] = {name = GetMedSchool.Institution, id = GetMedSchool.InstitutionID}>

</cfloop>

<cfdump var="#GetMedSchool#" label="Original Query">

<cfdump var="#result#" label="resulting array">

HTH,

--Dave

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 25, 2014 0
Community Beginner ,
Jul 25, 2014

Copy link to clipboard

Copied

I figured it out.

I had the   <cfset ArrayMedSchools = structNew()/> outside the loop instead of inside the loop.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 25, 2014 0
Community Beginner ,
Jul 25, 2014

Copy link to clipboard

Copied

Thanks everyone for the responses. This was nagging me for the past 2 days.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 25, 2014 0
Engaged ,
Jul 25, 2014

Copy link to clipboard

Copied

While your "solution" may have fixed your issue the code is still sub optimal.  You don't need to create a var for the struct and you have improper user of cfoutput.

--Dave

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 25, 2014 0