Search code examples
coldfusioncoldfusion-2016cfloop

ColdFusion cfloop issue


I'm running ColdFusion 2016. I have a cfloop which pulls in data from a query, all other ColdFusion queries work fine on the page and if I pull in the same variable in an output outside the loop, it works fine, except the loop is giving me an error. The error output says:

Variable GPS_LATITUDE is undefined.

Which is correct, as in the database there is no GPS_LATITUDE but there is a GPS_LATITUDE1.

I need to add the loop number on the end of the variable so as it loops pulls in the data gps_latitude1, gps_latitude2, gps_latitude3 not just gps_latitude.

My loop code is...

<cfoutput>
  <cfloop index="i" from="1" to="3">
    <td><p>#gps_latitude[i]#</p></td>
    <td><p>#gps_longitude[i]#</p></td>
  </cfloop>
</cfoutput>

Any guidance much appreciated.


Solution

  • @elixieru, You can't directly give gps_latitude[i]. which is not check your query which is consider it as Array. I can imagine your scenario and give my sample code about how to get an same columnName with

    <cfquery name='test' datasource="mytest">
        select * from test
    </cfquery>
    

    This is my sample query. It's having column name as address1, address2 etc... I'm going to get the data about address1 & address2 like your scenario.

    <cfloop query="test">
        <cfloop from="1" to="2" index="i">
            <cfset a = test["address#i#"]>
            <cfoutput> #a# </cfoutput> <br/>
        </cfloop>
    </cfloop>
    

    Here I'm looping over the query and so some simple / index loop based on my count ( Address1,2,3,4,5 etc ). For now I'm just use 2 like from 1 to 2.

    Here I've store the test['address#i#'] in variable a and print that variable. Now test['address#i#'] it will consider as test.address1

    I hope my sample help you more.