我试图弄清楚如何使用ColdFusion通过MS sql数据库运行查询,以便创建一个跟踪位置,总检查表百分比和位置总数的表。
我无法循环播放只显示一次我的位置并运行每个位置的总计。我不确定为什么我的桌子会添加所有这些线条,如下图所示,对此的任何帮助将不胜感激!
<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>
答案 0 :(得分:3)
您只需循环查询一次。删除其他cfoutput
<cfoutput query="allLocCode">
<cfset thisLocationName = trim(allLocCode.trans_location) />
<tr>
<td><strong>#thisLocationName#</strong></td>
<td></td>
<td></td>
</tr>
</cfoutput>
答案 1 :(得分:2)
您的代码会创建一个嵌套循环。
<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>
将行<cfoutput query="allLocCode">
更改为<cfoutput>
。