Search code examples
coldfusioncfmlcfloopcfif

How to output a message when query result is [empty string] in ColdFusion?


I have a query written in a stored procedure and the data is dumping just fine. Some of the results return [empty string] and in those cases, I'm trying to output a different message. Here's what I have thus far:

Looping through the query:

<cfloop query="#inactiveAdmins#">
  <tr>
    <td class="text-left">#Admin_Name#</td>
    <td class="text-left">#Dept_Name#</td>
    <td class="text-left">#Acad_Lead#</td>
    <td class="text-left">#Acad_Lead_Email#</td>
    <td class="text-right">#dateFormat(Last_Logon, 'mmm dd, yyyy')#</td>
  </tr>
</cfloop>

At the top of the page, I'm running this cfif statement.

<cfif #inactiveAdmins.Last_Logon# eq "">
  Never Logged On
<cfelse>
  #inactiveAdmins.Last_Logon#
</cfif>

But, in my output, I'm still getting a display with no message.

When I try to run the condition inside the loop, I get the following:

enter image description here


Solution

  • Is this supposed to be inside your table? Make sure you wrap it inside a tr and td.

    <cfloop query="#inactiveAdmins#">
      <tr>
        <td class="text-left">#Admin_Name#</td>
        <td class="text-left">#Dept_Name#</td>
        <td class="text-left">#Acad_Lead#</td>
        <td class="text-left">#Acad_Lead_Email#</td>
        <td class="text-right">#(Len(Last_Logon) ? dateFormat(Last_Logon, 'mmm dd, yyyy') : 'Never Logged On')#</td>
      </tr>
    </cfloop>
    
    

    Or the long form:

    <cfloop query="#inactiveAdmins#">
      <tr>
        <td class="text-left">#Admin_Name#</td>
        <td class="text-left">#Dept_Name#</td>
        <td class="text-left">#Acad_Lead#</td>
        <td class="text-left">#Acad_Lead_Email#</td>
        <td class="text-right">
            <cfif Len(Last_Logon)>
                #dateFormat(Last_Logon, 'mmm dd, yyyy')#
            <cfelse>
                Never Logged On
            </cfif>
        </td>
      </tr>
    </cfloop>