Why is cfoutput being ignored? - coldfusion

I have a very simple CFML page as follows:
<cfquery name="qry" datasource="#application.db.source#" username="#application.db.user#" password="#application.db.pass#">
SELECT * FROM changemgmt.rfc WHERE rfc_id = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="0">;
</cfquery>
<cfoutput query="qry">
#qry.RecordCount#
</cfoutput>
My <cfoutput> tag is not working as I would expect. If you were to look at the source code of this page, it would be entirely composed of empty lines. It's as if the <cfoutput> tag is being parsed out by the server. However if I change the code to:
<cfoutput>
#qry.RecordCount#
</cfoutput>
I am using Lucee as my backend CFML engine. Can anyone explain to my why there is a difference?

<cfoutput query="qry"> this is used to loop throw all the rows in the query. If your query result have no rows, the execution will never reach the login with in the <cfoutput query="qry">. But if there are 1+n rows in the result, your code will print the recordcount 1+n times.
In this case if all you want is to print recordcount of the result, then you just need to use <cfoutput>#qry#</cfoutput>
But if you want to print values each row of the query then you can use <cfoutput query="qry">
<cfoutput query="qry">
<div>
<span>#qry.rfc_id#</span>
<span>#qry.name#</span>
...
</div>
</cfoutput>

Related

ColdFusion cfloop issue

I'm running ColdFusion 2016. I have a cfloop which pulls in data from a query, all other ColdFusion queries work fine on the page and if I pull in the same variable in an output outside the loop, it works fine, except the loop is giving me an error. The error output says:
Variable GPS_LATITUDE is undefined.
Which is correct, as in the database there is no GPS_LATITUDE but there is a GPS_LATITUDE1.
I need to add the loop number on the end of the variable so as it loops pulls in the data gps_latitude1, gps_latitude2, gps_latitude3 not just gps_latitude.
My loop code is...
<cfoutput>
<cfloop index="i" from="1" to="3">
<td><p>#gps_latitude[i]#</p></td>
<td><p>#gps_longitude[i]#</p></td>
</cfloop>
</cfoutput>
Any guidance much appreciated.
#elixieru, You can't directly give gps_latitude[i]. which is not check your query which is consider it as Array. I can imagine your scenario and give my sample code about how to get an same columnName with
<cfquery name='test' datasource="mytest">
select * from test
</cfquery>
This is my sample query. It's having column name as address1, address2 etc... I'm going to get the data about address1 & address2 like your scenario.
<cfloop query="test">
<cfloop from="1" to="2" index="i">
<cfset a = test["address#i#"]>
<cfoutput> #a# </cfoutput> <br/>
</cfloop>
</cfloop>
Here I'm looping over the query and so some simple / index loop based on my count ( Address1,2,3,4,5 etc ). For now I'm just use 2 like from 1 to 2.
Here I've store the test['address#i#'] in variable a and print that variable. Now test['address#i#'] it will consider as test.address1
I hope my sample help you more.

ColdFusion - Output Grouping From a Query

I have a great question that is probably bone-head simple. I have the following query:
<cfquery name="getempareview" dbtype="query">
SELECT firstname,lastname,deptname,supcode
FROM getreviews
WHERE supcode IN (#preserveSingleQuotes(setsupcode)#)
</cfquery>
What I need to do is output so that the supcode has the other data under it in a list. So, if I have 100 rows of data and the supcode is the same on 25 of the records, just have the following:
supcode
firstname lastname - deptname (all 25 records would be listed out here)
Any help would be greatly appreciated.
Nested outputs. Try this.
<cfoutput query="YourQueryName" group="SupCode">
<h2>#SupCode#</h2>
<cfoutput>
#FirstName# #LastName# <br/>
</cfoutput>
</cfoutput>
You need to use nested and grouped output. And add an ORDER BY to your query.
<cfset setsupcode = "1,3,5">
<cfquery name="getempareview" dbtype="query">
SELECT firstname,lastname,deptname,supcode
FROM getreviews
WHERE supcode IN (<cfqueryparam value="#setsupcode#" cfsqltype="numeric" list="yes">)
ORDER BY supcode, deptname, lastname, firstname
</cfquery>
<cfoutput query="getempareview" group="supcode">
<h2>#supcode#</h2>
<cfoutput group="deptname">
#firstname# #lastname# (#deptname#) <br>
</cfoutput>
</cfoutput>
https://trycf.com/gist/763ede5485b0978504250f7f5baf9deb/acf11?theme=solarized_dark
Also, since this is apparently a Query of Query, you may be able to better organize your data in your initial query, rather than having to come back to reprocess the data.

Conditional CF Record Count

I'm trying to save my database server a few requests by querying a large number of items from several categories all at once, then using <cfif ... > statements to filter those results into unique tables I'm showing for each category. I'm looking to find record counts for each of the categories returned, not just the record count of the overall query.
The basic code is:
<cfinvoke component="..." method="..." returnvariable="session.queryList">
...
</cfinvoke>
<cfoutput #session.queryList#>
<cfif #category# eq "A">
[Table for A things]
</cfif>
<cfif #category# eq "B">
[Table for B things]
</cfif>
<cfif #category# eq "C">
[Table for C things]
</cfif>
</cfoutput>
I don't want to use "ORDER BY category" here because the tables are actually on different divs we're hiding and showing, so we need separate tables.
The problem I'm running into is that I want the "Table for A Things" to say "No results" if there is no records returned where category="A", but RecordCount seems to apply to the entire query. Is there any way to say something along the lines of <cfif #queryList.RecordCount# WHERE #category# eq "A" GT "0">?
QoQ can help.
<cfinvoke component="..." method="..." returnvariable="session.queryList">
...
</cfinvoke>
<!---then run QoQ on it--->
<cfquery name="catA" dbtype="query">
select * from session.queryList where category ="A"
</query>
<cfquery name="catB" dbtype="query">
select * from session.queryList where category ="B"
</query>
<cfif catA.recordcount>
<cfoutput query="catA">
[Table for A things]
</cfoutput>
<cfelse>
No Records for A things
</cfif>
<cfif catB.recordcount>
<cfoutput query="catB">
[Table for B things]
</cfoutput>
<cfelse>
No Records for B things
</cfif>
I believe you are trying to do the following. <cfoutput> has a feature that helps you group the query results given the query is ordered by the grouping item.
<cfoutput query="#session.queryList#" group="category">
<h3>Category #category#</h3>
<table>
<tr><th>...</th><th>...</th></tr>
<cfoutput><!--- This cfoutput loops through the each record in the group. --->
---rows---
</cfoutput>
<table>
</cfoutput>
QofQ is slow. You can accomplish this with a single round trip to mySQL:
SELECT someColumn, category, count(*) AS categoryCount
FROM theTable
GROUP BY category
ORDER BY category, someColumn
Grouping will give you a count per category which you can use in CFML.
<cfoutput query="session.queryList" group="category">
<cfif categoryCount eq 0>
No Records for #category#. =(
</cfif>
<cfoutput>
#someColumn#<br>
</cfoutput>
</cfoutput>

Group queries inside a cfloop

I'm looking to loop through a query, and would like to use grouping, like one would using cfoutput. I know CF10 added that support, but is there a script that emulates that behaviour so that items can be iterated easily?
Edit:
There are ways of getting around the lack of grouping in cfloop, by rearranging cfoutput tags, so they are not nested. The reason I'm looking for the cfloop workaround is that when nesting cfoutput, you need to use the results from the same query. I'd like to use my own QoQ and loop through the result.
OK, so you want to do this sort of thing:
<cfoutput query="query1">
<!--- stuff --->
<cfoutput query="query2" group="col>
<!--- more stuff --->
<cfoutput>
<!--- still more stuff --->
</cfoutput>
<!--- almost the last stuff --->
</cfoutput>
<!--- last stuff --->
</cfoutput>
?
And the second loop gives you an error:
Invalid tag nesting configuration.
A query driven cfoutput tag is nested inside a cfoutput tag that also has a query attribute. This is not allowed. Nesting these tags implies that you want to use grouped processing. However, only the top-level tag can specify the query that drives the processing.
You should be able to revise that to:
<cfloop query="query1">
<cfoutput>
<!--- stuff --->
</cfoutput>
<cfoutput query="query2" group="col>
<!--- more stuff --->
<cfoutput>
<!--- still more stuff --->
</cfoutput>
<!--- almost the last stuff --->
</cfoutput>
<cfoutput>
<!--- last stuff --->
</cfoutput>
</cfloop>
There's another option to emulate the group loop if you must. But that's a bunch of thinking and typing I'd rather avoid if poss, so let me know if this approach works first.

How to Break out of Cfoutput

I am looping through the results of a query, and I need to limit the number of rows displayed. I need to use cfoutput because I'm using the group attribute, and I cannot use the maxrows because not all rows will be displayed.
I tried to use <cfbreak> inside the <cfoutput>, but that throws an error.
How can I break out of the <cfoutput> loop?
If your group by is only there to remove duplicates from your results I would suggest using your query to cut them down then you can cfloop (select distinct and reducing your returned column list).
If you are using your group by to "group" your results You could run a counter within your loop and a cfif statement inside your first loop to omit later results.
You could fake the group by option in your cfloop by matching value from previous row if you need cfbreak
<cfloop query="queryname">
<cfif queryname.column[currentrow-1] neq queryname.column[currentrow]>
#queryname.column#
</cfif>
</cfloop>
Random note: you can maxrows on any/all levels of your grouped cfoutput
<cfset tmp = querynew('id,dd')>
<cfloop from="1" to="20" index="i">
<cfset queryaddrow(tmp,1)>
<cfset querysetcell(tmp,'id',rand(),i)>
<cfset querysetcell(tmp,'dd',(i mod 4),i)>
</cfloop>
<cfquery dbtype="query" name="tmp">select * from tmp order by dd</cfquery>
<cfoutput query="tmp" group="dd" maxrows="2">#dd#<br
<ul>
<cfoutput maxrows="2" group="id"><li>#id#</li></cfoutput>
</ul>
</cfoutput>
You could use the cfthrow tag to trigger an exception that will allow you to break out of the loop using cfcatch you can then ignore the exception and continue processing. That will give you what you want.
<cftry>
<cfset i = 0>
<cfoutput query="qMyQuery" group="someGroup">
<cfset i = i + 1>
Parent
<cfoutput>
Child
</cfoutput>
<cfif i GTE 10>
<cfthrow type="break">
</cfif>
</cfoutput>
<cfcatch type="break">
<!--- DO NOTHING - THIS IS A HACK FOR NOT BEING ABLE TO USE CFBREAK inside cfoutput. --->
</cfcatch>
</cftry>