CFLoop Error For Missing Entries

后端 未结 2 1726
独厮守ぢ
独厮守ぢ 2021-01-25 04:12

please disregard this post. I have made a clearer example of my problem here: Error with CFLoop When Entries Are Missing

I am running the CFLoop code be

相关标签:
2条回答
  • 2021-01-25 04:35

    Using the recordcount of the external database in the loop should prevent the errors.

    <cfloop index="x" from="1" to="#ExternalDatabaseQuery.RecordCount#">
    

    A better solution, assuming you do have the query in memory, would be to use a query of queries.

    <cfquery dbtype='query' name='data'>
    SELECT SID, First AS FirstName, Last AS LastName, Age AS StudentAge
    FROM ExternalDatabaseQuery
    </cfquery>
    
    0 讨论(0)
  • 2021-01-25 04:51

    Mike, apologies if I'm misunderstanding what you're after but it would seem that some basic conditionals could do the job. The following edit to your code is just a suggestion as to how to go about it (your full code base may dictate something slightly different, of course).

    <cfset data = queryNew("sid,firstname,lastname,age","integer,varchar,varchar,integer")>
    <cfloop index="x" from="1" to="50">
        <cfif isDefined("first[x]") AND isDefined("last[x]") AND isDefined("studentage[x]")>
        <cfset queryAddRow(data)>
        <cfset querySetCell(data,"sid",x)>
        <cfset querySetCell(data,"firstname","#first[x]#")>
        <cfset querySetCell(data,"lastname","#last[x]#")>
        <cfset querySetCell(data,"age","#studentage[x]#")>
        </cfif>
    </cfloop>
    
    <cfoutput query="data">
        #sid# -  #firstnamet# #lastname# - #age#<br />
    </cfoutput>
    

    The primary problem here (and maybe this is not an issue for you) is that this will output 50 - errors. So, if there are 3 errors (i.e., no data found) you'll have 47 entries output rather than 50. If that's an issue, please add a comment ... there are some alternative approaches for ensuring that you always have 50 items output.

    0 讨论(0)
提交回复
热议问题