-1

I have a .cfc on my server that I use to run a query and send the result back to a phonegap app. I cannot get the syntax right to check if any data has been returned in a query and send back a string in the result like "no data found" to display. here's my code...

remote array function getintList() {
        var q = new com.adobe.coldfusion.query();
        q.setDatasource("myData");
        q.setSQL("select id1, Date, ShowLInk, IntName, description from myData Where intyear = #dateformat(Now(), "YYYY")# order by date desc");
        var data = q.execute().getResult();
        var result = [];
        for(var i=1; i<= data.recordCount; i++) {
            arrayAppend(result, {"id"=data.id1[i], "name"=data.IntName[i], "date"=dateformat(data.date[i], "mmmm d, yyyy"), "description"=data.description[i], "showlink"=data.ShowLInk[i]});
        }
        return result;
    }

Thought maybe I could do a cfif statement like this but it doesn't work...

<cfif data.recordcount lt 1>
result = "no data"
return result;
<cfelse>
return result;
</cfif>

Hope someone can help me.

ZnArK
  • 1,533
  • 1
  • 12
  • 23

2 Answers2

5

Well results is an array.

If your in phone gap I assume then javascript is handling the result then simply use javascript length on the result.

ie

if(result.length){
    //have data doStuff
}else{
    //noData
}
Paul
  • 1,577
  • 7
  • 10
  • This worked great! I was totally in the wrong place thinking it needed to be in my .cfc. Thanks for steering me in the right direction! – Phil Baumert Jul 02 '12 at 17:01
1

Your function returns an array. So you just need to check for arraylen() on your returnvariable. For example:

<!--- invoke your function --->
<cfset intlist = createobject("component","yourcomponentname").getintlist() />

<!--- check for resulsts --->
<cfif arraylen(intlist)>
  do something with your results ...
<cfelse>
  No results ...
</cfif>

EDIT: oh, just didn't saw that your function is meant to be remotely invoked. So its probably called via ajax. You'll just check for .length in JavaScript:

// assumed that "data" holds your JSON result
if(data.length) { 
  // do something with the results
} else {
  // show "no results"-message
}
Seybsen
  • 14,989
  • 4
  • 40
  • 73