缺少条目的CFLoop错误 [英] CFLoop Error For Missing Entries

查看:192
本文介绍了缺少条目的CFLoop错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请忽略此信息。我在这里做了一个更清楚的例子我的问题:
CFLoop的错误当条目缺失

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

我正在运行下面的CFLoop代码。

I am running the CFLoop code below.

<cfset data = queryNew("sid,firstname,lastname,age","integer,varchar,varchar,integer")>
<cfloop index="x" from="1" to="50">
    <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]#")>
</cfloop>

<cfoutput query="data">
    #sid# -  #firstnamet# #lastname# - #age#<br />
</cfoutput>

变量 first [x] code>最后[x] 和 studentage [x] X 是循环索引。注意,CFLoop有50个条目。

The variables first[x], last[x], and studentage[x] are being pulled from an external datasource, with X being the loop index. Note that the CFLoop has 50 entries.

当有数据可用时,代码工作得很漂亮。但是,当有缺失数据时,代码断开。我的意思是如果条目11没有列出为第一[x] 变量的名称我得到一个错误沿着元素第一行未定义,错误发生在第5行

When there is data available, the code works beautifully. However, when there is missing data, the code breaks. By that I mean if Entry 11 doesn't have a name listed for the first[x] variable I get an error along the lines of "Element first is undefined. The error occurred on line 5

(第5行是名字的条目)。

(line 5 is the entry for first name).

当发生这种情况时,我想从我的结果中省略条目11(以及导致错误的所有其他条目),并防止错误显示。

When this happens, I would like to omit entry 11 (and all other entries which cause an error) from my results and prevent the error from showing. How can I do this?

澄清:请假设数据已定义,它使用外部数据源时有点毛,但我要说的是条目1到10

Clarification: Please assume the data is defined. It gets a bit hairy since I am using an external datasource. But what I am saying is that Entries 1 to 10 show up. When its entry 11's turn, that's when the error comes up.

推荐答案

Mike,如果我误解了什么你的后面,但似乎一些基本的条件句可以做这项工作。下面的编辑你的代码只是一个建议如何去(你的完整的代码库可能会规定一些略有不同,当然)。

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>

这里的主要问题(也许这不是一个问题) - 错误。所以,如果有3个错误(即,没有找到数据),您将有47项输出,而不是50.如果这是一个问题,请添加评论...有一些替代方法,以确保您始终有50项输出。

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.

这篇关于缺少条目的CFLoop错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆