ColdFusion - 使用CFLOOP动态创建列名 [英] ColdFusion - Creating column names dynamically with CFLOOP

查看:119
本文介绍了ColdFusion - 使用CFLOOP动态创建列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  TABLE tblDocuments 
COLUMNS documentID(int,not null,pk)
document1(varchar(250),null)
document2(varchar(250),null)
/ *并继续通过* /
document14因此,我查询特定记录的任何文档:

pre> < cfquery name =qryGetDocsdatasource =#dsn#>
SELECT document1,... document14
FROM tblDocuments
WHERE documentID =< cfqueryparam name =SESSION.documentIDcfsqltype =cf_sql_integer>
< / cfquery>

表单看起来像这样:

 < form name =frmUploadDocsmethod =postaction =documentsPage.cfm> 

< input type =filename =document1size =50>
< cfif qryGetDocs.document1 IS NOT''>
(当前文件名:< a href =#vars.file_path#/#qryGetDocs.document1#>#qryGetDocs.document1#< / a>)< / cfif>

< input type =filename =document2size =50>
< cfif qryGetDocs.document2 IS NOT''>
(当前文件名:< a href =#vars.file_path#/#qryGetDocs.document2#>#qryGetDocs.document2#< / a>)< / cfif>

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

< input type =filename =document14size =50>
< cfif qryGetDocs.document14 IS NOT''>
(当前文件名:< a href =#vars.file_path#/#qryGetDocs.document14#>#qryGetDocs.document14#< / a>)< / cfif>

< input type =submitname =submitvalue =上传文件>
< / form>

我想从1循环到14,所以我只有一个 < input> < cfif> 语句,如下所示:

 < cfloop from =1to =14index =i> 
< input type =fiilename =document#i#size =30>
< cfif qryGetDocs.document#i#IS NOT''>
(当前文件名:< a href =#vars.file_path#/#qryGetDocs.document [#i#]#>#qryGetDocs.document [#i#]#< / a>)
< / cfif>
< / cfloop>

但是,无论我试过什么,我都无法获得正确的语法。有人可以帮我这个吗?

解决方案

(原来的问题已经回答了,只是为了说明...)



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

  TABLE:tblDocuments 
COLUMNS:DocumentID(唯一记录ID)
UserID
DocumentName

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

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

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

 < cfoutput query =qryGetDocs > 
...
< input type =filename =document#CurrentRow#size =50>
< input type =hiddenname =documentID#CurrentRow#value =#documentID#>
(当前文件名:< 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 =filename =document#counter#size =50>
< input type =hiddenname =documentID#counter#value =0>
< / cfloop>
< / cfoutput>


I have a table that records the name of uploaded documents, up to 14 per record. The columns are named thus:

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

So I query for any documents for a particular record:

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

The form looks something like this:

<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>

I want to loop from 1 to 14, so that I only have one <input> and <cfif> statement, like so:

<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>

However, I cannot get the syntax correct no matter what I've tried. Can someone please help me with this? Thank you!

解决方案

(The original question was already answered. But just to illustrate ...)

A more flexible structure is to store the documents as rows. So basic table might be:

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

Using this structure, you could retrieve all existing documents for a single user with a simple query

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

.. and display them with a simple output loop. (Note, I added "documentID" as hidden field to identify existing documents ..)

<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>

If the query contains less than 14 files (or whatever your maximum is..), you can use the query.recordCount to determine how many additional file inputs to display.

<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动态创建列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆