ColdFusion查询获取当前行值?

时间:2017-03-03 14:28:39

标签: sql sql-server-2008 coldfusion coldfusion-9 cfquery

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

<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语句被忽略/不正确。如果有人知道我的问题在哪里或者解决这个问题的方法不同,请告诉我。

2 个答案:

答案 0 :(得分:3)

您是否确实需要使用值列表预先填充字段,或者只是在操作页面上生成结果?

如果您只需要产生该结果,则无需做任何特殊的事情。只需创建多个具有相同名称的字段即可。结果将是操作页面上每种类型的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 

答案 1 :(得分:1)

你可以尝试更像这样的东西......

<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>
相关问题