ColdFusion 查询获取当前行值?

ColdFusion query get current row values?

我有一个可以 return 多条记录的查询。我的查询中有两列,一列输出日期值,第二列是类型。我想检查每一行的类型并在列表中输出日期。由于某种原因,我当前的代码在同一个输入字段中输出所有日期值,这不是我想要的。这是我的代码:

<cfquery name="getUserRec" datasource="MyDBone">
    SELECT CONVERT(VARCHAR(10), u_begDt, 101) AS u_begDt, u_type    
    FROM Users WITH (NOLOCK)
    WHERE u_uid = <cfqueryparam value="#uid#" cfsqltype="cf_sql_char" maxlength="15">
        AND u_type IN ('A','C','M','S')
</cfquery>

查询将产生这样的记录:

u_begDt     u_type
03/16/2017    A 
03/01/2017    C 
03/01/2017    S
03/16/2017    M
02/01/2013    S
07/16/2015    A

现在我想在 4 个单独的输入字段中输出这些记录:

<cfoutput>
   <input type="hidden" name="begDtA" id="begDtA" value="<cfif trim(getUserRec.u_type) EQ 'A'>#ValueList(getUserRec.u_begDt,",")#</cfif>" readonly="readonly" />
   <input type="hidden" name="begDtC" id="begDtC" value="<cfif trim(getUserRec.u_type) EQ 'C'>#ValueList(getUserRec.u_begDt,",")#</cfif>" readonly="readonly" />
   <input type="hidden" name="begDtM" id="begDtM" value="<cfif trim(getUserRec.u_type) EQ 'M'>#ValueList(getUserRec.u_begDt,",")#</cfif>" readonly="readonly" />
   <input type="hidden" name="begDtS" id="begDtS" value="<cfif trim(getUserRec.u_type) EQ 'S'>#ValueList(getUserRec.u_begDt,",")#</cfif>" readonly="readonly" />
</cfoutput>

我当前的代码将在同一个隐藏字段中输出所有日期值,看起来我的 cfif 语句是 ignored/incorrect。如果有人看到我的问题在哪里或解决这个问题的不同方法,请告诉我。

您可以尝试更多类似的东西...

<cfoutput>
<cfloop query="getUserRec">
  <cfif trim(u_type) EQ 'A'>
    <input type="hidden" name="begDtA" id="begDtA" value="#ValueList(u_begDt,",")#" readonly="readonly" />
  </cfif>
  <cfif trim(u_type) EQ 'C'>
    <input type="hidden" name="begDtC" id="begDtC" value="#ValueList(u_begDt,",")#" readonly="readonly" />
  </cfif>
</cfloop>
</cfoutput>

您是否真的需要使用 列表 值预先填充字段,还是只在操作页面上生成该结果?

如果你只需要产生那个结果,那么不需要做任何特别的事情。只需创建多个具有相同名称的字段。结果将是操作页面上每种类型的 csv 列表。

<cfoutput query="getUserRec">
    <input type="text" name="begDt#getUserRec.u_type#"  
         value="#dateFormat(getUserRec.u_begDt, 'mm/dd/yyyy')#" />
</cfoutput> 

如果您确实需要用值列表预填充字段,请使用分组 cfoutput。将您的数据库查询修改为 order by u_type。 (无需格式化 SQL 中的日期。将其留给前端代码)。然后使用分组的 cfoutput 为每个 u_type 构建一个值列表。

<cfoutput query="getUserRec" group="u_type">
    <cfset dates = []>
    <cfoutput>
        <cfset arrayAppend(dates, dateFormat(getUserRec.u_begDt, "mm/dd/yyyy"))>
    </cfoutput> 
    <input type="text" name="begDt#getUserRec.u_type#" value="#arrayToList(dates)#" />
</cfoutput> 

结果:

BEGDTA  03/01/2015,03/16/2017
BEGDTC  03/01/2017
BEGDTM  03/16/2017
BEGDTS  02/01/2013,03/01/2017