如何显示 ColdFusion 查询中的每个元素
我编写了这段 CF 代码来获取并显示数据库中的数据。 (实际上是为了填充文本字段。)问题:我没有从查询中获取值,但我正确地获取了记录数。
我应该如何通过 cfloop 访问查询返回的值?下面是我的作品。
<cfquery name="data_query" datasource="#dsn#">
SELECT
id,
name
FROM learning
</cfquery>
<cfloop query=data_query">
<li>
<div class="list_div clearfix">
<input type="text" value="#URLDecode(name)#">
</div>
</li>
</cfloop>
</cfquery>
I have written this piece of CF code to get and display the data from database. (Actually to populate text fields.) The problem: I am not getting values from the query, but I am correctly getting the number of records.
How am I supposed to access the values returned by the query via a cfloop? Below is my work.
<cfquery name="data_query" datasource="#dsn#">
SELECT
id,
name
FROM learning
</cfquery>
<cfloop query=data_query">
<li>
<div class="list_div clearfix">
<input type="text" value="#URLDecode(name)#">
</div>
</li>
</cfloop>
</cfquery>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您有两个选择:
使用
标签包裹 vars 输出行:将
使用
循环而不是使用
You have two options:
Wrap the vars output line with
<cfoutput />
tags:<cfoutput>#id#: <input type="text" value="#name#"></cfoutput>
Use
<cfoutput query="data_query">
loop instead of<cfloop ...>
For the sake of cleaner code I would prefer the second option so your code would be:
此外,在输出时,您还应该正确确定查询列的“范围”。这将使您的代码将来更容易维护,例如,您将始终知道#data_query.name#属于查询,而不是由某处其他代码段设置的字符串。它会加快页面性能 - 如果您不设置变量范围(这适用于所有类型的变量,而不仅仅是查询),那么 CF 将循环遍历不同的范围,直到找到具有该值的内容。因此,通过范围界定,您可以防止 CF 必须循环。
Also you should properly 'scope' your query columns when outputting. This will make your code easier to maintain in future, e.g. you'll always know that #data_query.name# belonged to the query and wasn't some string set by some other piece of code somewhere. And it'll speed up page performance - if you don't scope variables (this applies to all types of variables, not just queries), then CF will loop through the different scopes until it finds something with this value. So by scoping, you prevent CF having to loop.
总的来说,你的逻辑很好......只是一些错别字和需要小的改变......
尝试一下。
On the whole your logic was fine.. just a few typos and minor changes needed..
Give this a try.
如果您不知道这一点:
OR
将为您提供从查询或任何变量或结构中返回的精美显示。
And if you didn't know about it:
OR
Will give you a beautiful display of came back from your query, or in any variable or structure.