2011-08-25 44 views
0

我有以下ColdFusion代碼從數據庫獲取信息並在主頁上顯示結果。這裏的CFQUERY代碼:cfloop空查詢條件?

<cfquery name="getSchedule" datasource="#APPLICATION.datasource#" dbtype="odbc"> 
SELECT * FROM SCHEDULE_Days SD 
LEFT JOIN SCHEDULE_ScheduledClasses SSC ON SD.day_id = SSC.day_id 
LEFT JOIN SCHEDULE_Classes SC ON SSC.class_id = SC.class_id 
WHERE SD.day_date = #createODBCDate(now())# AND SSC.schedule_cancelled = 0 
ORDER BY SSC.start_time 
</cfquery> 

和輸出代碼:

<cfoutput> 
<cfloop query="getSchedule"> 
<tr> 
<td width="40">&nbsp;</td> 
<td width="74">#lcase(timeFormat(start_time,"h:mm tt"))#</td> 
<td width="158">#class_name#</td> 
</tr> 
</cfloop> 
</cfoutput> 

的問題是,如果沒有包含在getSchedule數據(即沒有ScheduledClasses),它什麼也不顯示。

我正在尋找一種方法來改變這種情況,以便在沒有要顯示的數據的情況下,我可以指定消息和代碼以顯示其缺席狀態。

+1

閱讀CFQUERY的文檔(或者甚至CFLOOP,因爲有覆蓋它的腳註的註釋)將允許您解答這爲你自己。 -1,對不起。 –

+0

坦誠地讚賞亞當 - 我繼承了這個代碼庫,並且不能不瞭解ColdFusion。 – Nietzsche

回答

11

首先只是一個快速的CF提示你可以讓你的代碼通過做這種方式更好:

<cfif getSchedule.recordcount GT 0> 
    <cfoutput query="getSchedule"> 
    <tr> 
     <td width="40">&nbsp;</td> 
     <td width="74">#lcase(timeFormat(getSchedule.start_time,"h:mm tt"))#</td> 
     <td width="158">#getSchedule.class_name#</td> 
    </tr> 
    </cfoutput> 
<cfelse> 
    <p>Empty record message here</p> 
</cfif> 

我之所以把查詢輸出放在第一位最有可能發生這種情況比使用空集信息更多。

+0

這將產生無效的HTML - 你不能在'TABLE'中有'P'。可能想用' ...味精...'而不是。 –

0

使用的RecordCount檢測查詢是否有任何記錄


<cfif getSchedule.recordcount gt 0> 
.... do something 
</cfif> 
1
<cfif getSchedule.recordcount> 
.... do something 
</cfif> 

的工作只是藏漢沒有必要對GT 0

0
<cfif getSchedule.RecordCount> 
<table> 
<cfoutput query="getSchedule"> 
<tr> 
<td width="40">&nbsp;</td> 
<td width="74">#lcase(timeFormat(start_time,"h:mm tt"))#</td> 
<td width="158">#class_name#</td> 
</tr> 
</cfoutput> 
</table> 
<cfelse> 
<p>There are currently no records</p> 
</cfif>