【问题标题】:How to Modify CFLoop to Output Items By Date如何修改 CFLoop 以按日期输出项目
【发布时间】:2014-02-06 10:34:06
【问题描述】:

我有一个包含一系列日期和其他详细信息的查询。目前,如果查询返回 4 个日期,例如(2/27/20142/28/20143/1/20143/2/2014),我的代码会循环遍历所有日期并将相应的项目放在面板上正确日期的下方。因此,它会生成 4 个面板,每个面板都包含一个日期。

如何调整代码以输出 2 个面板(每个面板有 2 个日期),而不是 4 个面板,每个面板包含一个日期?谢谢。

old_date = "";
<cfloop query="getItinerary">
   <cfset cur_date = dateFormat(start_date,"m/d/yy")>
   <cfif old_date NEQ cur_date>
    <cfif old_date NEQ "">
        <cfoutput>
            CODE TO END PANEL AND START A NEW ONE       
            </cfoutput>
    </cfif>
    <cfoutput>
        ITINERARY ITEM DATE     
    </cfoutput>     
   </cfif>

   <cfoutput>
    ITINERARY ITEM INFO DETAILS
   </cfoutput>  
</cfloop>
    <cfoutput>
        CODE TO END PANEL
    </cfoutput>

【问题讨论】:

  • 您能提供更多的输出 (HTML) 代码吗?我有一个解决方案,但如果可能的话,我想在您的实际输出代码的上下文中提供它。 OTW,我想看看什么完全代表包含“两个日期”的“面板”。甚至模拟一些所需的示例输出也会有所帮助。

标签: coldfusion cfloop


【解决方案1】:

您是否尝试过这样的查询?它将一次显示 2 个记录结果集。

<cfif getItinerary.CurrentRow MOD 3>  
...

【讨论】:

  • 模组 3?你不是说mod 2吗?
  • 也用过这个...谢谢
【解决方案2】:

我认为这个问题还没有完全回答,所以我会试一试。 您要做的是使用模数运算符将数据分成块,在本例中为面板。这可能看起来很简单,但它可能很棘手,因为 ColdFusion 的基本索引为 1。因此,使用可以在 Java 等其他语言中工作的逻辑,使用基本索引为零的逻辑并不像预期的那样工作。这是我相信您正在寻找的代码:

  <!---Note that CMFL/ColdFusion programming is not my thing so the syntax may be a bit off, however the logic should work but I'm sure an actual ColdFusion programmer can write more optimal code--->

<!---we start by setting a locally scoped variable to use as a counter in base 0 --->
<cfset var iCount = 0>
<!--- loop over all the items in the query --->
<cfloop query="getItinerary">
    <!--- if the current value of iCount is divisible by 2 then proceed--->
    <cfif iCount MOD 2 EQ 0>
        <!---first time around 0 mod 2 = 0--->
        <cfif iCount EQ 0>
            <!--- insert the opening panel tag--->
            [insert panel opening tag(s)]
        <cfelseif iCount < getItinerary.recordCount>
             <!---its not the first panel and it is not the last record --->
             [insert closing tag(s) of previous panel]
             [insert opening tag(s) of next panel]
        </cfelse>             
        </cfif>
    </cfif>
    <cfoutput>
        <!--- insert the date you would like listed within the panel --->
        #dateFormat(getItinerary.start_date,"m/d/yy")#
    </cfoutput>
    <cfset iCount = iCount + 1>
    <!---check if that was the last record, if so, close last the panel--->
    <cfif iCount EQ getItinerary.recordCount>
         [insert panel closing tag(s)]
    </cfif>
</cfloop>

请注意,上面的代码假定至少有一条记录。如果没有记录是可能的,请相应地添加代码。同样使用带有基数 1 索引的 mod 2 会在第一个面板上为您提供错误的日期数。此外,mod 3 会在第一个面板上为您提供 2 个日期,然后在所有其他面板上为您提供 3 个日期(尽管在这种情况下,如果限制为 4,则 base 1 mod 3 可能有效,但如果更改限制,它将中断)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-07
    • 1970-01-01
    相关资源
    最近更新 更多