如何显示 ColdFusion 查询中的每个元素

发布于 2025-01-01 03:24:45 字数 468 浏览 4 评论 0原文

我编写了这段 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

神经暖 2025-01-08 03:24:45

您有两个选择:

  1. 使用 标签包裹 vars 输出行:

    #id#:

  2. 使用 循环而不是

使用

<cfquery name="data_query" datasource="#dsn#">
    SELECT 
        id,
        name
    FROM learning
</cfquery> 

<cfoutput query="data_query">
    <li>
        <div class="list_div clearfix">
            #id#: <input type="text" value="#name#">
        </div>
    </li>
</cfoutput>

You have two options:

  1. Wrap the vars output line with <cfoutput /> tags:

    <cfoutput>#id#: <input type="text" value="#name#"></cfoutput>

  2. 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:

<cfquery name="data_query" datasource="#dsn#">
    SELECT 
        id,
        name
    FROM learning
</cfquery> 

<cfoutput query="data_query">
    <li>
        <div class="list_div clearfix">
            #id#: <input type="text" value="#name#">
        </div>
    </li>
</cfoutput>
や莫失莫忘 2025-01-08 03:24:45

此外,在输出时,您还应该正确确定查询列的“范围”。这将使您的代码将来更容易维护,例如,您将始终知道#data_query.name#属于查询,而不是由某处其他代码段设置的字符串。它会加快页面性能 - 如果您不设置变量范围(这适用于所有类型的变量,而不仅仅是查询),那么 CF 将循环遍历不同的范围,直到找到具有该值的内容。因此,通过范围界定,您可以防止 CF 必须循环。

<cfquery name="data_query" datasource="#variables.dsn#">
    SELECT 
        id,
        name
    FROM learning
</cfquery> 

<cfoutput query="data_query">
    <li>
        <div class="list_div clearfix">
            #data_query.id#: <input type="text" value="#data_query.name#">
        </div>
    </li>
</cfoutput>

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.

<cfquery name="data_query" datasource="#variables.dsn#">
    SELECT 
        id,
        name
    FROM learning
</cfquery> 

<cfoutput query="data_query">
    <li>
        <div class="list_div clearfix">
            #data_query.id#: <input type="text" value="#data_query.name#">
        </div>
    </li>
</cfoutput>
苦笑流年记忆 2025-01-08 03:24:45

总的来说,你的逻辑很好......只是一些错别字和需要小的改变......

尝试一下。

<cfquery name="data_query" datasource="#dsn#">
    SELECT 
        id,
        name
    FROM learning
</cfquery> 

<cfloop query="data_query">
    <li>
        <div class="list_div clearfix">
            #id#: <input type="text" value="#name#">
        </div>
    </li>
</cfloop>

On the whole your logic was fine.. just a few typos and minor changes needed..

Give this a try.

<cfquery name="data_query" datasource="#dsn#">
    SELECT 
        id,
        name
    FROM learning
</cfquery> 

<cfloop query="data_query">
    <li>
        <div class="list_div clearfix">
            #id#: <input type="text" value="#name#">
        </div>
    </li>
</cfloop>
浅语花开 2025-01-08 03:24:45

如果您不知道这一点:

<cfdump var="#data_query#">

OR

<cfdump var="#data_query#" abort>

将为您提供从查询或任何变量或结构中返回的精美显示。

And if you didn't know about it:

<cfdump var="#data_query#">

OR

<cfdump var="#data_query#" abort>

Will give you a beautiful display of came back from your query, or in any variable or structure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文