質問

I have the following ColdFusion code that is getting information from a database and displaying the results on the homepage. Here's the cfquery code:

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

and the output code:

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

The issue is, if there is no data contained within getSchedule (i.e., there are no ScheduledClasses), it displays nothing.

I'm looking for a way to change this so that, in the event that there is no data to display, I can specify a message and code to be shown in its absence.

役に立ちましたか?

解決

First just a quick CF tip you can make you code better by doing it this way:

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

The reason I put the query output first is most likely this will happen more than with your empty set message.

他のヒント

<cfif getSchedule.recordcount>
.... do something
</cfif>

Will work just aswell there is no need for gt 0

Use the recordCount to detect whether the query has any record


<cfif getSchedule.recordcount gt 0>
.... do something
</cfif>
<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>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top