Coldfusion CF位置查询循环问题
我试图弄清楚如何使用ColdFusion在MS sql数据库中运行查询,以便生成一个跟踪位置、总检查列表百分比和位置总数的表 我很难循环显示我的位置只有一次,并运行每个位置的总数。我不知道为什么我的表格会添加所有这些行,如下图所示,对此的任何帮助都将不胜感激Coldfusion CF位置查询循环问题,coldfusion,Coldfusion,我试图弄清楚如何使用ColdFusion在MS sql数据库中运行查询,以便生成一个跟踪位置、总检查列表百分比和位置总数的表 我很难循环显示我的位置只有一次,并运行每个位置的总数。我不知道为什么我的表格会添加所有这些行,如下图所示,对此的任何帮助都将不胜感激 <cfset result = {} /> <cftry> <cfquery datasource="#application.dsn#" name="GetLocationInfo">
<cfset result = {} />
<cftry>
<cfquery datasource="#application.dsn#" name="GetLocationInfo">
SELECT *
FROM cl_checklists
</cfquery>
<cfcatch type="any">
<cfset result.error = CFCATCH.message >
<cfset result.detail = CFCATCH.detail >
</cfcatch>
</cftry>
<table border="1" id="Checklist_Stats">
<thead>
<th><strong>Location</strong></th>
<th><strong>Percent of Total Checklists</strong></th>
<th><strong>Location Total</strong></th>
</thead>
<tbody>
<cfquery name="allLocCode" dbtype="query">
SELECT DISTINCT trans_location, COUNT(*) AS locCntr FROM GetLocationInfo GROUP BY trans_location ORDER BY trans_location
</cfquery>
<cfloop query="allLocCode">
<cfset thisLocationName = trim(allLocCode.trans_location) />
<cfoutput query="allLocCode">
<tr>
<td><strong>#thisLocationName#</strong></td>
<td></td>
<td></td>
</tr>
<cfset thisLocationName = "" />
</cfoutput>
</cfloop>
</tbody>
<!--- Total of All Sum of each column --->
<tr>
<td><strong>Total</strong></td>
<td></td>
<td></td>
</tr>
</table>
您的代码创建了一个嵌套循环
<cfloop query="allLocCode"> // loop over all items in the query
<cfset thisLocationName = trim(allLocCode.trans_location) />
<cfoutput query="allLocCode"> // loop again over all items in the query
<tr>
<td><strong>#thisLocationName#</strong></td> // print the string
<td></td>
<td></td>
</tr>
<cfset thisLocationName = "" /> // empties the string, hence next rows will be empty
</cfoutput>
</cfloop>
将行更改为。您的代码将创建一个嵌套循环
<cfloop query="allLocCode"> // loop over all items in the query
<cfset thisLocationName = trim(allLocCode.trans_location) />
<cfoutput query="allLocCode"> // loop again over all items in the query
<tr>
<td><strong>#thisLocationName#</strong></td> // print the string
<td></td>
<td></td>
</tr>
<cfset thisLocationName = "" /> // empties the string, hence next rows will be empty
</cfoutput>
</cfloop>
将行更改为。您只需循环查询一次。卸下额外的CFO输出
您只需要循环查询一次。卸下额外的CFO输出
在查询中循环两次在查询中循环两次在循环中关闭cfoutput是不必要的处理。将标记移到cfloop之外,或者去掉cfloop。在循环内部打开或关闭cfoutput是不必要的处理。将标记移到cfloop之外,或者去掉cfloop。