checking the existence while printing value in a table - coldfusion

I am displaying results in a table after looping through a query. For TestNumber, there are some results in my query where the number is not present and
hence I want to display N/A instead of just blank in the table. So, I am checking the existence
using IsDefined, but for some reason it keeps on printing N/A everytime.
<cfloop query="GetMyList1">
<tr>
<td align="center">#TestName#</td>
<cfif IsDefined(TestNumber) >
<td align="center">#TestNumber#</td>
<cfelse>
<td align="center">N/A</td>
</cfif>
<td align="center">#Date#</td>
</tr>
</cfloop>

You would want to check if there is a length of the item. The field exists, so isDefined will always return true
<cfloop query="GetMyList1">
<tr>
<td align="center">#GetMyList1.TestName#</td>
<td align="center"><cfif len(trim(GetMyList1.TestNumber))>#GetMyList1.TestNumber#<cfelse>N/A</cfif></td>
<td align="center">#GetMyList1.Date#</td>
</tr>
</cfloop>

Related

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

How can I leave the loop if no records are part of the if statement?

Below code works, but I would like to show 'no records' message if
<cfif GetResults2.csedept_id eq aFieldValue> has no records for that value. I have tried to put a counter on, but I just can't get it to show 'no records' and not show the
<thead> <th>Name</th> <th>Positive Comment</th> <th>Negative Comment</th></thead> heading of the table if there are no records.
How can I show "No Records" and hide the table head if the results come back empty
Right now if the result comes back empty it will say "no results"(correct) and display the header(incorrect).
<cfset counter3= 0>
<table cellpadding="0" cellspacing="0" class="tablecolors">
<h2> Comments </h2>
<thead> <th>Name</th> <th>Positive Comment</th> <th>Negative Comment</th></thead>
<cfloop query="GetResults2">
<cfif GetResults2.csedept_id eq aFieldValue>
<tr>
<td nowrap="nowrap">#emp_namefirst# #Left(emp_namelast, 1)# </td>
<td>#Replace(commentpositive, emp_namefirst, "<B>" & emp_namefirst & "</B>")#</td>
<td>#Replace(commentnegative, emp_namefirst, "<B>" & emp_namefirst & "</B>")#</td>
</tr>
<cfelse><p>no records</p>
</cfif>
</cfloop>
</table>
UPDATE: Just to add I do have another query above like #FRANK said that does pretty much the same things for example:
'<cfloop query="GetEmployeeTotals3">
<cfif GetEmployeeTotals3.csedept_id eq aFieldValue> '
here is the query:
select GetResults.* , GetEmployees.emp_namefirst, GetEmployees.emp_namelast
from GetResults, GetEmployees
where employee = emp_id
order by csedept_id
so all the solutions above that I tried won't work.
I would recommend using a query of queries to whittle the result set down to just the values you care about first. Then you can easily check, the recordcount before outputting the table at all.
Alternatively, loop first and build up the results with cfsavecontent, then check to see if you found any prior to getting to the table bits.
Something like this should work. You'll have to set a flag of showRecords that will determine if you should show the headers.
<cfset showRecords = false>
<cfloop query="GetResults2">
<cfif GetResults2.csedept_id eq aFieldValue>
<cfset showRecords = true>
<cfbreak>
</cfif>
</cfloop>
<h2> Comments </h2>
<cfif showRecords>
<table cellpadding="0" cellspacing="0" class="tablecolors">
<thead> <th>Name</th> <th>Positive Comment</th> <th>Negative Comment</th></thead>
<cfloop query="GetResults2">
<cfif GetResults2.csedept_id eq aFieldValue>
<tr>
<td nowrap="nowrap">#emp_namefirst# #Left(emp_namelast, 1)# </td>
<td>#Replace(commentpositive, emp_namefirst, "<B>" & emp_namefirst & "</B>")# </td>
<td>#Replace(commentnegative, emp_namefirst, "<B>" & emp_namefirst & "</B>")#</td>
</tr>
</cfif>
</cfloop>
</table>
<cfelse>
<p>no records</p>
</cfif>
EDIT: Updated answer after question was clarified
You'll need to move your cfif to include your table head when checking for records. If the check comes back with a record count, then output the table head as well as the results, otherwise output "no results". I've written two versions below, one is utilizing the cfoutput instead of a loop (just personal preference) and one with your loop if you wish to keep it.
I added Dan's listfind() valuelist() combo...so give him credit for that.
Edit Edit so this is the super-strength/ultra-correct awesome solution with the 'I-am-tired-of-editing-this-answer-50-times-to-get-OP-an-answer', with bonus shameless theft from Dan's answer above/below. Or where ever we ending up order-wise.
<table>
<cfif getresults2.recordcount AND ListFind(ValueList(GetResults2.csedept_id), aFieldValue)>
<tr>
<th>name</th>
<th>positive comment</th>
<th>negative comment</th>
</tr>
<cfoutput query="getresults2">
<tr>
<td nowrap="nowrap">#emp_namefirst# #left(emp_namelast, 1)# </td>
<td>#replace(commentpositive, emp_namefirst, "<b>" & emp_namefirst & "</b>")#</td>
<td>#replace(commentnegative, emp_namefirst, "<b>" & emp_namefirst & "</b>")#</td>
</tr>
</cfoutput>
<cfelse>
<tr colspan="3">
<td><p>no records</p></td>
</tr>
</cfif>
</table>
If you have your cfoutput somewhere in your code (where we cannot see), then here is your cfloop back, simply replace this where I have the cfoutput in the snippet above.
<cfloop query="getresults2">
<tr>
<td nowrap="nowrap">#emp_namefirst# #left(emp_namelast, 1)# </td>
<td>#replace(commentpositive, emp_namefirst, "<b>" & emp_namefirst & "</b>")#</td>
<td>#replace(commentnegative, emp_namefirst, "<b>" & emp_namefirst & "</b>")#</td>
</tr>
</cfloop>
While your question is still unclear, this might be what you want.
<cfif ListFind(ValueList(GetResults2.csedept_id), aFieldValue)>
your existing code to display results in a table
<cfelse>
<p>No Records Found</p>
Note that this:
ListFind(ValueList(GetResults2.csedept_id), aFieldValue)
will execute and return false even if GetResults2 has no records at all.
Edit starts here
Based on this comment, "well if it only appears on some then display does but to those who do not match then ignore" change your existing code to something like:
<cfquery name = "q3" dbtype = "query">
select * from GetResults2
where csedept_id = #aFieldValue#
<cfquery>
<table>
column header row
<cfoutput query="q3">
data rows
</cfoutput>
<table>
The initial construct at the start of this answer still applies.

html parsing using jsoup and coldfusion

This is the continuation of my previous question. Below is the script I am trying to build to parse HTML which looks like the example below. I am getting the error Value must be initialised before use. Not able to attached the error.
I have to make a http call using jsoup where I need to provide username and password for the server login. Is the below code right way to do it? I looked at the Bennals blog for html parsing using jsoup.
I have this in my Application.cfc
component {
this.name = "jsoupTest";
this.javaSettings = {loadPaths=["/jsoup/jsoup-1.7.3.jar"], loadColdFusionClassPath=true};
}
Example of the HTML to be parsed
Note there are at least 5000 rows like below which need to be parsed and extract only the TEXT from the TD.
<tbody>
<tr>
<td class="drpdetailtablerowdetailleft">Robert M Best Jr.</td>
<td class="drpdetailtablerowdetailleft">AAI</td>
<td class="drpdetailtablerowdetail">7948</td>
<td class="drpdetailtablerowdetail">1</td>
<td class="drpdetailtablerowdetail">MC</td>
<td class="drpdetailtablerowdetail">Compliant</td> <td class="drpdetailtablerowdetail">Compliant</td> <td class="drpdetailtablerowdetail">Compliant</td> <td class="drpdetailtablerowdetail">Compliant</td> <td class="drpdetailtablerowdetail">Compliant</td> <td class="drpdetailtablerowdetail">Compliant</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="drpdetailtablerowdetailleft">Robert M Best Jr.</td>
<td class="drpdetailtablerowdetailleft">ABWS</td>
<td class="drpdetailtablerowdetail">4884</td>
<td class="drpdetailtablerowdetail">4</td>
<td class="drpdetailtablerowdetail">NMC</td>
<td class="drpdetailtablerowdetail">Compliant</td> <td class="drpdetailtablerowdetail">Compliant</td> <td class="drpdetailtablerowdetail">Compliant</td> <td class="drpdetailtablerowdetail">Compliant</td> <td class="drpdetailtablerowdetail">Compliant</td> <td class="drpdetailtablerowdetail">Compliant</td>
</tr>
</tbody>
Updated Code to be used
<cfhttp url="https://intra.att.com/itscmetrics/EM2/LTMR.cfm" method="get" username="abc" password="zxyr">
<cfhttpparam type="url" name="LTMX" value="Andre Fuetsch / Shelly K Lazzaro">
</cfhttp>
<cfset jsoup = createObject("java", "org.jsoup.Jsoup") />
<cfset document = jsoup.parse(myPage.filecontent) />
<cfset content = doc.getElementById("contentwrapper")>
<!--- Let's see what we got. --->
<cfdump var="#content#" />
The myPage variable is being declared for the first time in your parse command.
I think you need to add result="myPage" to your cfhttp call.
<cfhttp result="myPage" url="https://intra.att.com/itscmetrics/EM2/LTMR.cfm" method="get" username="abc" password="zxyr">
It looks like the reason it is not working is because you have not called the constructor on the Jsoup class.
Try changing this line
var jSoupClass = createObject( "java", "org.jsoup.Jsoup" ).init(); // note calling init calls the constructor for the Java class
Did you install your jar file correctly?
ColdFusion searches for the objects in the following order:
The ColdFusion Java Dynamic Class Load directories:
Java archive (.jar) files in web_root/WEB-INF/lib
Class (.class) files in web_root/WEB-INF/classes
Quoted from : About ColdFusion, Java, and J2EE
So copy your jar file to web_root/WEB-INF/lib, restart CF, and try again.

How to loop through array and output in multiple columns?

I need help looping through my data and outputting it into multiple columns. Currently, it displays vertically right under the next set of data in the array. Please help.
<cfloop array="#UserAddresses#" index="UA">
<tr>
<td>City: </td>
<td><cfif NOT Len(#UA.city.xmlText#)>N/A<cfelse><cfoutput>#UA.city.xmlText#</cfoutput></cfif></td>
</tr>
<tr>
<td>State: </td>
<td><cfif NOT Len(#UA.state.xmlText#)>N/A<cfelse><cfoutput>#UA.state.xmlText#</cfoutput></cfif></td>
</tr>
<tr>
<td>Zip Code: </td>
<td><cfif NOT Len(#UA.zipcode.xmlText#)>N/A<cfelse><cfoutput>#UA.zipcode.xmlText#</cfoutput></cfif></td>
</tr>
</cfloop>
</cfif>
You want the City, State and Zip Code to be headers? Then this would work
<cfoutput>
<tr>
<td>City: </td>
<td>State: </td>
<td>Zip Code: </td>
</tr>
<cfloop array="#UserAddresses#" index="UA">
<tr>
<td><cfif NOT Len(UA.city.xmlText)>N/A<cfelse>#UA.city.xmlText#</cfif></td>
<td><cfif NOT Len(UA.state.xmlText)>N/A<cfelse>#UA.state.xmlText#<</cfif></td>
<td><cfif NOT Len(UA.zipcode.xmlText)>N/A<cfelse>#UA.zipcode.xmlText#</cfif></td>
</tr>
</cfloop>
</cfoutput>
It works because <tr></tr> defines a table row and <td></td> defines a cell in that row. In your case, you were doing multiple rows without cells, thus you were getting your content in a column, instead of a row. A side note is that <cfoutput></cfoutput> should be used only once per page and not around every variable.

cfloop empty query condition?

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