ColdFusion - 使用 CFLOOP 动态创建列名称

标签 coldfusion coldfusion-8

我有一个表,记录上传的文档名称,每条记录最多14个。这些列的命名如下:

TABLE tblDocuments
COLUMNS documentID (int, not null, pk)
        document1 (varchar(250), null)
        document2 (varchar(250), null)
        /* and this continues through */
        document14 (varchar(250), null)

因此,我查询特定记录的任何文档:

<cfquery name="qryGetDocs" datasource="#dsn#">
     SELECT document1, ...document14
     FROM   tblDocuments
     WHERE  documentID = <cfqueryparam name="SESSION.documentID" cfsqltype="cf_sql_integer">
</cfquery>

表格看起来像这样:

<form name="frmUploadDocs" method="post" action="documentsPage.cfm">

<input type="file" name="document1" size="50" >
<cfif qryGetDocs.document1 IS NOT ''>
   (current file name: <a href="#vars.file_path#/#qryGetDocs.document1#">#qryGetDocs.document1#</a>)</cfif>

<input type="file" name="document2" size="50" >
<cfif qryGetDocs.document2 IS NOT ''>
   (current file name: <a href="#vars.file_path#/#qryGetDocs.document2#">#qryGetDocs.document2#</a>)</cfif>

<!--- list all documents --->

<input type="file" name="document14" size="50" >
<cfif qryGetDocs.document14 IS NOT ''>
   (current file name: <a href="#vars.file_path#/#qryGetDocs.document14#">#qryGetDocs.document14#</a>)</cfif>

<input type="submit" name="submit" value="Upload Documents">
</form>

我想从1循环到14,这样我就只有一个<input><cfif>声明,如下所示:

<cfloop from="1" to="14" index="i">
   <input type="fiile" name="document#i#" size="30">
   <cfif qryGetDocs.document#i# IS NOT ''>
     (current file name: <a href="#vars.file_path#/#qryGetDocs.document[#i#]#">#qryGetDocs.document[#i#]#</a>)
   </cfif>
</cfloop>

但是,无论我尝试什么,我都无法获得正确的语法。有人可以帮我解决这个问题吗?谢谢!

最佳答案

(原来的问题已经得到解答。但只是为了说明......)

更灵活的结构是将文档存储为行。所以基本表可能是:

TABLE:    tblDocuments
COLUMNS:  DocumentID  (unique record id)
          UserID
          DocumentName

使用此结构,您可以通过简单的查询检索单个用户的所有现有文档

<cfquery name="qryGetDocs" datasource="#dsn#">
     SELECT documentID, documentName
     FROM   tblDocuments
     WHERE  userID = <cfqueryparam name="#SomeUserIDVariable#" cfsqltype="cf_sql_integer">
</cfquery>

.. 并使用简单的输出循环显示它们。 (注意,我添加了“documentID”作为隐藏字段来识别现有文档..)

<cfoutput query="qryGetDocs">
   ...
   <input type="file" name="document#CurrentRow#" size="50" >
   <input type="hidden" name="documentID#CurrentRow#" value="#documentID#" >
   (current file name: <a href="#vars.file_path#/#documentName#">#documentName#</a>)
</cfoutput>

如果查询包含的文件少于 14 个(或者您的最大值......),您可以使用 query.recordCount 来确定有多少个文件 要显示的其他文件输入。

<cfset nextInputNumber = qryGetDocs.recordCount + 1>
<cfoutput>
<cfloop from="#nextInputNumber#" to="#MaximumNumberOfDocs#" index="counter">
   <input type="file" name="document#counter#" size="50" >
   <input type="hidden" name="documentID#counter#" value="0" >
</cfloop>
</cfoutput>

关于ColdFusion - 使用 CFLOOP 动态创建列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3918923/

相关文章:

coldfusion - 在从 CF9 迁移到 Railo 之前,我应该考虑哪些事项?

coldfusion - esapi:对于 src 属性,我们应该使用 encodeForHTMLAttribute 吗?编码ForURL?或两者?

coldfusion - 如何将 QueryAddRow() 中添加的行作为查询结果的第一行?

coldfusion - 如何在 Coldfusion 中找到 {CFIDE-HOME}

iis - 通过 SSL 在 IE 上使用 CFContent

javascript - 屏幕尺寸检测后的冷融合

database - 循环或输出中的最后和第一个内容(冷融合)

服务对话框中不存在 ColdFusion 服务

postgresql - 在 CF Administrator 中管理证书

coldfusion - 在 MS Word 中单击的链接丢失 CF session 变量,但复制和粘贴工作正常