coldfusion查询函数的问题在查询循环内调用

时间:2011-12-15 22:43:17

标签: coldfusion

我有一个查询循环,在该循环内部,我进行cfc函数调用以返回第二个查询。它用显示器做了奇怪的事情:

在下面的示例中,我创建了一个简单的循环,而不是进行函数调用以获取第二个查询。这显示正常。 View the actual page here

<cfoutput>
    <cfif qCal.recordcount>
        <a class="control" onClick="return hs.getExpander(this).printHtml()" href="##">Print</a>
    </cfif>
    <table width="100%" cellspacing="0" cellpadding="4">
        <cfloop query="qCal">
            <tr>
                <td align="middle" valign="top" style="width:150px;">
                    <a title="View full-sized image" onclick="return hs.expand(this)" href="/images/classes/#qCal.image#" class="highslide"><img src="/images/classes/#qCal.thumb#" class="resize2"></a></td>
                <td>
                    <table cellspacing="0" cellpadding="3">
                        <tr>
                            <td colspan="2" class="textNormal"><h2>#qCal.title#</h2></td>
                        </tr>
                        <tr>
                            <td class="textNormal"><strong>Date:</strong></td>
                            <td class="textNormal"><strong>Time:</strong></td>
                        </tr>
                        <cfloop from="1" to="5" index="i"> <!--- basic loop --->
                            <tr>
                                <td>#i#</td>
                                <td></td>
                            </tr>
                        </cfloop>   
                    </table>

                </td>
            </tr>
        </cfloop>
   </table> 
</cfoutput>

但是,只要我进行函数调用以获取辅助查询,显示就会发生莫名其妙的事情。 View the actual page here(提供半熟的html代码)。

<cfoutput>
    <cfif qCal.recordcount>
        <a class="control" onClick="return hs.getExpander(this).printHtml()" href="##">Print</a>
    </cfif>
    <table width="100%" cellspacing="0" cellpadding="4">
        <cfloop query="qCal">
            <cfsilent>
                <cfset qCalItems = o_system.getCalendarItems(
                      start=dateformat(vStartDate,"yyyy-mm-yy"),
                      end=dateformat(vEndDate,"yyyy-mm-yy"),
                      classID=qCal.classID,
                      forQuarterlyCalendar=true,
                      order_by="i.startDate")>
            </cfsilent>
            <tr>
                <td align="middle" valign="top" style="width:150px;">
                    <a title="View full-sized image" onclick="return hs.expand(this)" href="/images/classes/#qCal.image#" class="highslide"><img src="/images/classes/#qCal.thumb#" class="resize2"></a></td>
                <td>
                    <table cellspacing="0" cellpadding="3">
                        <tr>
                            <td colspan="2" class="textNormal"><h2>#qCal.title#</h2></td>
                        </tr>
                        <tr>
                            <td class="textNormal"><strong>Date:</strong></td>
                            <td class="textNormal"><strong>Time:</strong></td>
                        </tr>
                        <cfloop query="qCalItems">
                            <tr bgcolor="#iif(qCalItems.CurrentRow MOD(2) eq 1,de('ffffff'),de('EFEFEF'))#">
                                <td>#DayOfWeekAsString(dayofweek(qCalItems.startDate))# #dateformat(qCalItems.startDate,"dd mmm yyyy")#</td>
                                <td>#qCalItems.startTime#-#qCalItems.endTime#</td>
                            </tr>
                        </cfloop>
                    </table>
                </td>
            </tr>
        </cfloop>
   </table> 
</cfoutput>

更新 - 添加了CFC代码

注意:函数返回结构或查询,具体取决于参数&#34; forQuarterlyCalendar&#34;。在这种情况下,它肯定会返回一个查询对象。我已经转出查询并确认它是一个有效的查询对象。

<cffunction name="getCalendarItems" access="remote" returntype="any" output="false" returnformat="json">
        <cfargument name="classID" type="any" required="false" default="">
        <cfargument name="forSelect" type="boolean" required="false" default="false">
        <cfargument name="forQuarterlyCalendar" type="boolean" required="false" default="false">
        <cfargument name="start" type="any" required="false" default="">
        <cfargument name="end" type="any" required="false" default="">
        <cfargument name="order_by" type="any" required="false" default="c.title">
        <cfargument name="sort_direction" type="any" required="false" default="asc">
        <cfargument name="json" type="boolean" required="false" default="true">
        <cfargument name="must_have_store_item" type="boolean" required="false" default="true">
        <cfset var qClass = 0>
        <cfset var realStart = "">
        <cfset var realEnd = "">
        <cfset var results = []>
        <cfset var vUrl = "">
        <cfset var vId = "">
        <cfset var vTitle = "">
        <cfset var vStart = "">
        <cfif len(trim(arguments.start))>
            <cfset realStart = EpochTimeToLocalDate(arguments.start)>
        </cfif>
        <cfif len(trim(arguments.end))> 
            <cfset realEnd = EpochTimeToLocalDate(arguments.end)>
        </cfif>

        <cfquery name="qClass" datasource="#application.datasource#">
            select  <cfif arguments.forSelect>
                        c.title, i.calendarItemID as id, convert(varchar, i.startDate, 103)+' '+i.startTime+'-'+i.endTime as text 
                    <cfelseif arguments.forQuarterlyCalendar>
                        c.description, i.calendarItemID as id, i.startDate, i.startTime, i.endTime 
                    <cfelse>
                        i.calendarItemID, i.startDate, i.startTime, i.endTime
                        ,c.classID, c.title, c.description, c.price, c.places, c.exclusive, c.discounted, c.url
                        ,s.storeItemID 
                    </cfif>
            from calendarItem i
            <cfif arguments.must_have_store_item>
                join storeItem s on s.entityID = i.calendarItemID and s.storeItemTypeID = 3 
            </cfif>
            join class c on c.classID = i.classID and c.active=1
            where i.active=1
            <cfif isnumeric(arguments.classID)>
                and i.classID = <cfqueryparam value="#arguments.classID#" cfsqltype="cf_sql_integer">
            </cfif>
            <cfif len(trim(arguments.start))>
                <cfif arguments.forQuarterlyCalendar>
                    and i.startDate >= <cfqueryparam value="#arguments.start#" cfsqltype="cf_sql_date">
                <cfelse>
                    and i.startDate >= <cfqueryparam value="#dateformat(realStart,'yyyy-mm-dd')#" cfsqltype="cf_sql_date">
                </cfif>
            </cfif>
            order by #arguments.order_by# #arguments.sort_direction#
        </cfquery>
        <cfif qClass.recordcount>
            <cfif not arguments.forSelect and not arguments.forQuarterlyCalendar>
                <cfloop query="qClass">
                    <cfset vUrl = qClass["url"]>
                    <cfset vId = qClass["storeItemID"]> 
                    <cfset vTitle = qClass["title"]>
                    <cfset vStart = GetEpochTimeFromLocal(qClass.startDate)>
                    <cfif not len(trim(url)) or len(trim(url)) is 0>
                        <cfset vUrl = "#application.webroot#/class_detail.cfm?id=#vId#">
                    </cfif>
                    <cfset s = structnew()>
                    <cfset s["id"] = vId>
                    <cfset s["url"] = vUrl>
                    <cfset s["title"] = vTitle>
                    <cfset s["start"] = vStart>
                    <cfset arrayappend(results, s)> 
                </cfloop>
            <cfelse>
                <cfset results = qClass>
            </cfif>
        </cfif>
        <cfif arguments.json>
            <cfcontent type="application/json">
        </cfif>
        <cfreturn results>
    </cffunction>

1 个答案:

答案 0 :(得分:2)

你需要变换范围's':

<cfset s = structnew()>