双重评估 - 如何根据保存列名称的变量访问我的查询列?
我有一个查询和一个字段/列名称列表。我想做一种双循环 - 循环查询中的每个记录,然后循环字段/列名称列表并输出每个相应的字段。循环应该是这样的:
<table>
<cfoutput query="myQuery">
<tr>
<cfloop list="#cols#" index="col">
<td>?</td>
</cfloop>
</tr>
</cfoutput>
</table>
问题是在问号所在的位置放置什么...我已经尝试过#myquery[col]#
,但这不起作用。我需要获取变量 col
中的字符串名称指示的变量...显然,#col#
将只返回列名称。我需要找出某种方法来双重评估字符串...类似于 ##col##
,这当然也行不通。我怎样才能做到这一点?
I have a query and a list of field/column names. I want to do a sort of double loop - loop through each record in the query, and then loop through the list of field/column names and output each corresponding field. The loops should be something like this:
<table>
<cfoutput query="myQuery">
<tr>
<cfloop list="#cols#" index="col">
<td>?</td>
</cfloop>
</tr>
</cfoutput>
</table>
The problem is what to put where the question mark is... I've tried #myquery[col]#
, but this didn't work. I need to get the variable indicated by the string name in the variable col
... And obviously, #col#
will just return the column name. I need to figure out some way to double-evaluate the string... something like ##col##
, which of course won't work either. How can I accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当将列名作为结构引用时,您还需要告诉查询您想要获取哪一行。如果您没有通过 myQuery.ColumnList 获取 cols 变量,您还应该确保检查列名是否存在。
使用以下代码动态引用循环中的每一列:
When referencing column names as a structure, you need to also tell the query which row you want to get. You should also make sure that you check that the column name exists if you didn't get the cols variable via myQuery.ColumnList.
Use the following code to dynamically reference each column in your loop:
您仍然可以将 Sergii 的方法用于您自己的专栏列表:
You can still use Sergii's approach with your own columnlist:
知道了!! :)
Got it!! :)