Search code examples
coldfusioncoldfusion-9record-count

ColdFusion RecordCount as multiple conditional


I would like to run one query + output and in case no records exist run second query + output and in case both have no records then redirected to

<test1.RecordCount eq 0>

but the problem is that I can only use one <cfelse>

Any idea?

The Code for one query + output and RecordCount

<cfif test1.RecordCount eq 0>
    <!--- Display some message.--->
<cfelse>
    <cfoutput query="test1">
        <!--- Display some other message --->
    </cfoutput>
</cfif>

Solution

  • If I understand your question correctly, you should be able to use nested <cfif ...> conditions.

    Something like:

    <cfif test1.RecordCount gt 0>
        <cfoutput query="test1">
            <!--- Display test1 query results --->
        </cfoutput>
    <cfelse>
        <cfif test2.RecordCount gt 0>
            <cfoutput query="test2">
                <!--- Display test2 query results --->
            </cfoutput>
        <cfelse>
            <!--- Display some message.--->
        </cfif>
    </cfif>
    

    Or you could use <cfelseif> like this:

    <cfif test1.RecordCount gt 0>
        <cfoutput query="test1">
            <!--- Display test1 query results --->
        </cfoutput>
    <cfelseif test2.RecordCount gt 0>
        <cfoutput query="test2">
            <!--- Display test2 query results --->
        </cfoutput>
    <cfelse>
        <!--- Display some message.--->
    </cfif>