• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Not getting all results in Array Populated from Query

Guest
Jul 25, 2014 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">

Views

305

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
community guidelines

correct answers 1 Correct answer

Deleted User
Jul 25, 2014 Jul 25, 2014

I figured it out.

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

Votes

Translate

Translate
Engaged ,
Jul 25, 2014 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.

Votes

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
community guidelines
Engaged ,
Jul 25, 2014 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

Votes

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
community guidelines
Guest
Jul 25, 2014 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.

Votes

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
community guidelines
Guest
Jul 25, 2014 Jul 25, 2014

Copy link to clipboard

Copied

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

Votes

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
community guidelines
Engaged ,
Jul 25, 2014 Jul 25, 2014

Copy link to clipboard

Copied

LATEST

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

Votes

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
community guidelines
Resources
Documentation