CFLoop Error For Missing Entries

我怕爱的太早我们不能终老 提交于 2019-12-02 07:29:24

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>

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!