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

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

问题描述

请忽略此帖.我在这里做了一个更清楚的例子来说明我的问题:条目丢失时 CFLoop 出错

我正在运行下面的 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]last[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 没有为 first[x] 变量列出名称,我会收到一个错误,类似于 "Element first is undefined. The error occurred on line5

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 出现了.轮到它的条目 11 时,就会出现错误.

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>

这里的主要问题(也许这对您来说不是问题)是这将输出 50 - 错误.因此,如果有 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天全站免登陆