質問

I have a query that contains a series of dates, and other details. Currently, if the query returns 4 dates, say (2/27/2014, 2/28/2014, 3/1/2014, 3/2/2014), my code loops through all days and puts the corresponding items under its correct day on a panel. So, it generates 4 panels each containing a single date.

How can I adjust the code so it outputs 2 panels (with 2 dates each), instead of 4 panels each containing a single date? Thanks.

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>
役に立ちましたか?

解決 2

I don't think this question has been quite answered completely so I will give it a try. What you want to do is use the modulus operator to break your data into chunks, in this case panels. This may seem straight forward but it can be tricky because ColdFusion has a base index of 1. So using logic that would work in other languages like Java, which use a base index of zero doesn't quite work as expected. Here is the code I believe you are looking for:

  <!---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>

Note that the code above assumes that there is at least one record. If no records is a possibility, add code accordingly. Also using mod 2 with a base 1 index will give you the wrong number of dates on the first panel. Also, mod 3 would give you 2 dates on the first panel and 3 dates on all other panels afterwards (although in this case since the limit if 4, base 1 mod 3 may work but it would break if the limit was changed).

他のヒント

Have you tried something like this for you query? It will display 2 record result sets at a time.

<cfif getItinerary.CurrentRow MOD 3>  
...
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top