嵌套的cfloops记录少于外循环导致"数组索引超出范围"错误

时间:2014-09-08 08:17:12

标签: coldfusion coldfusion-10 cfloop

我很好奇为什么会这样。我现在已经遇到过两次了,经过大量的谷歌搜索后,我找不到任何我理解的理由。它的要点:

查询1:selectContent(6条记录;没有空格/空值等)

查询2:selectPricing(5条记录;没有空格/空值等)

输出:

<cfloop query="selectContent">
    <section>
        #selectContent.h2#
        <cfif selectContent.id eq 3>
            <cfloop query="selectPricing" group="groupCol">
                <table class="pricing">
                <thead>
                    <tr>
                        <th>#description#</th>
                        <th>Price</th>
                    </tr>
                </thead>
                <tbody>
                    <cfloop>
                    <tr>
                        <td>#selectPricing.description#</td>
                        <td>#selectPricing.price#</td>
                    </tr>
                    </cfloop>
                </tbody>
                </table>
            </cfloop>
        </cfif>
        #selectContent.content#
    </section>
</cfloop>

这将产生以下错误:数组索引超出范围:5

仅当第二个查询的记录少于第一个查询时才会出现错误。基本上感觉就像第一个cfloop从第二个接管循环迭代,这导致了问题,但也只有当你有第三个分组cfloop。整个内部cfloop运行,就像源中一样。

我想出了两种解决方法:

  • 使用cfoutput / group执行此操作,但这相对比较难看,因为它意味着从页面的其他部分大量关闭cfoutputs。
  • 如果currentRow与recordcount匹配,则在第三个cfloop上粘贴cfbreak。

所以,有两个问题:

  • 为什么会发生这种情况?

  • 我应该在这里采用一种完全不同的方法(谷歌/所以没有找到这个问题的其他人似乎肯定暗示......)

修改 我已根据Adam Cameron的反馈将此作为Coldfusion错误提交。 Bug #3820049

1 个答案:

答案 0 :(得分:5)

干得好,你在CF中发现了一个错误。我可以复制它(PS ......如果你包含了一些样本数据,那就很酷了,除非我必须这样做!)

但解决方法很简单:

<cfscript>
selectContent = queryNew("h2,id,content", "varchar,integer,varchar", [
    ["one", 1, "content.1"],
    ["two", 2, "content.2"],
    ["three", 3, "content.3"],
    ["four", 4, "content.4"],
    ["five", 5, "content.5"],
    ["six", 6, "content.6"],
    ["seven", 7, "content.7"]
]);

selectPricing = queryNew("groupCol,description,price", "varchar,varchar,varchar", [
    ["groupCol.1", "description.1", "1.11"],
    ["groupCol.2", "description.2", "2.22"],
    ["groupCol.2", "description.3", "3.33"],
    ["groupCol.3", "description.4", "4.44"],
    ["groupCol.3", "description.5", "5.55"],
    ["groupCol.3", "description.6", "6.66"]
]);

</cfscript>
<cfloop query="selectContent">
    <section>
        <cfoutput>#selectContent.h2#</cfoutput>
        <cfif selectContent.id eq 3>
            <cfoutput query="selectPricing" group="groupCol">
                <table class="pricing">
                <thead>
                    <tr>
                        <th>#description#</th>
                        <th>Price</th>
                    </tr>
                </thead>
                <tbody>
                    <cfoutput>
                    <tr>
                        <td>#description#</td>
                        <td>#price#</td>
                    </tr>
                    </cfoutput>
                </tbody>
                </table>
            </cfoutput>
        </cfif>
        <cfoutput>#selectContent.content#</cfoutput>
    </section>
</cfloop>

请注意我是如何使用<cfoutput>进行内部循环的。

这是ColdFusion(10和11)中的一个严重错误,你应该在bug base上提出它(如果你这样做,请在这里报告票号/ URL,这样我们就可以对它进行投票)