SQL 数组作为数字返回,而不是 ColdFusion 中的字符串
在 Coldfusion 中,我使用一个 cfc 将一个选择框绑定到另一个选择框(基本上,从一个框中选择一个州,第二个框填充县名称。)县框的值是一个 5 位数字,即格式化为文本(即值来自文本字段。)
问题是我发现如果所选县 ID 的值以“0”开头,则它已被截断。
所以我得到类似的东西: 艾达县 11223A 2300 B(应该是02300)
有人可以帮助确保前导0不会被切断吗?
这是页面上的选择框:
<!--- State Name options --->
<b>State:</b><br />
<cfselect bind="cfc:states.getStates()" bindonload="true" name="search_state" id="search_state" value="StateUSAbb" display="StateName">
</cfselect><br />
<!--- County Name options --->
<b>County:</b><br />
<cfselect bind="cfc:states.getCounties({search_state})" name="search_county" id="search_county" value="FIPS_County" display="CountyName">
</cfselect>
我讨厌粘贴整个 .cfc,但要注意后面的部分,特别是使用 cfset 填充数组的 cfloop 结果:
<cfcomponent output="false">
<!--- Get array of media types --->
<cffunction name="getStates" access="remote" returnType="array">
<!--- Define variables --->
<cfset var data="">
<cfset var result=ArrayNew(2)>
<cfset var i=0>
<!--- Get data --->
<cfquery name="data" datasource="bridges">
SELECT DISTINCT tblLoc.StateUSAbb, lkuState.StateName
FROM lkuState INNER JOIN tblLoc ON lkuState.FIPS_State = tblLoc.FIPS_State
WHERE (lkuState.StateName <> 'New Brunswick')
UNION
SELECT '' AS StateUSAbb, ' ALL' AS StateName
FROM lkuState
ORDER BY StateName
</cfquery>
<!--- Convert results to array --->
<cfloop index="i" from="1" to="#data.RecordCount#">
<cfset result[i][1]=data.StateUSAbb[i]>
<cfset result[i][2]=data.StateName[i]>
</cfloop>
<!--- And return it --->
<cfreturn result>
</cffunction>
<!--- Get counties by state --->
<cffunction name="getCounties" access="remote" returnType="array">
<cfargument name="stateabb" type="string" required="true">
<!--- Define variables --->
<cfset var data="">
<cfset var result=ArrayNew(2)>
<cfset var i=0>
<!--- Get data --->
<cfquery name="data" datasource="bridges">
SELECT '' AS FIPS_COUNTY, ' ALL' as CountyName
FROM lkuCnty
UNION
SELECT FIPS_County, CountyName
FROM lkuCnty
WHERE StateAbb = '#ARGUMENTS.stateabb#'
ORDER BY CountyName
</cfquery>
<!--- Convert results to array --->
<cfloop index="i" from="1" to="#data.RecordCount#">
<cfset result[i][1]=data.FIPS_County[i]>
<cfset result[i][2]=data.CountyName[i]>
</cfloop>
<!--- And return it --->
<cfreturn result>
</cffunction>
</cfcomponent>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在数字末尾添加一个空格,那么 CF 会将其视为字符串,并且不会截掉前导 0。
简单的解决方法:
顺便说一句,您知道支持查询对象来填充
cfselect
对吧?所以你甚至不需要循环。您可以执行相同的解决方法,但在 cfqueryUPDATE: 内的 SQL 中,无论如何,想法是,如果您想保留前导 0 并继续使用 CF 内置的 serializeJSON() 或调用 cfc 远程方法在 JSON 样式中(将在内部调用serializeJSON(),您可以附加一个空格,以便 CF 会将其视为字符串,并保留前导 0。如果您的脚本必须需要“012345”且没有尾随空格,则寻找另一个来自 riaforge 或 cflib 的 JSON 序列化器。
append a space to the end of the number, then CF will treat it as a string and no leading 0 will be chopped.
simple workaround:
<cfset result[i][1]=data.StateUSAbb[i] & " ">
btw, you know that query object is supported for populating
cfselect
right? So you don't even need the loop. You can do the same workaround but in SQL inside your cfqueryUPDATE: anyway, the idea is that if u want to preserve the leading 0 and keep using CF's built in serializeJSON() or calling the cfc remote method in JSON style (which will internally invoke serializeJSON(), you can append a space so CF will treat it as a string and leading 0 will be preserved. If your script somehow must need "012345" with no trailing space, then look for another JSON seralizer from riaforge or cflib.
您确定从查询返回的数据是包含前导零的文本字符串,而不仅仅是整数值?无论如何,我认为 Zachary 对 NumberFormat(x, "00000") 的建议是正确的选择。
You're sure the data returned from your query is a text string which contains the leading zero, rather than just the integer value? Regardless, I think Zachary's suggestion of NumberFormat(x, "00000") is the way to go.
如果数据是固定长度,您可以使用 NumberFormat 强制前导零。一般来说,CF 是无类型的,因此必须发生一些导致数据损坏的底层转换。您可以尝试强制值 toString(),或者调试添加单引号之类的内容作为列值中的第一个字符(例如 SELECT '''' + FIPS_County, '''' + CountyName FROM lkuCnty)以查看是否他们保留了所有的性格。
[更新]
根据您对 SQL 如何不返回 5 个字符的评论,使用此更新的查询从 INT 转换为带有前导零的 VARCHAR。
If the data is a fixed length you can use NumberFormat to force leading zero's. In general, CF is typeless so there must be some underlying conversion happening that is causing the data to get corrupt. You might try forcing the value toString(), or to debug add something like a single quote as the first character in the column value (eg. SELECT '''' + FIPS_County, '''' + CountyName FROM lkuCnty) to see if they keep all their characters.
[Update]
Based on your comments about how SQL is not returning 5 char, use this updated query to go from INT to VARCHAR with leading zeros.