2017-03-03 51 views
1

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

<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语句被忽略/不正确。如果有人看到我的问题或不同的方式来解决这个问题,请让我知道。

+0

如果你真的需要一个字段为每种类型的,请尝试使用分组输出。虽然为什么不用这些名称生成多个字段?这将在操作页面上生成所需的CSV列表。另外,哪个DBMS? – Leigh

+0

你能提供任何例子吗? –

+0

哪一个?您是否确实需要每种类型的单个表单字段,或者在操作页面上为每种类型都有单个值列表? – Leigh

回答

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

由于我必须验证/比较JavaScript中每个u_type的日期,我认为第二种解决方案将是更好的选择。谢谢你的帮助! –

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

(Typo编辑)ValueList()仍将使用所有值填充隐藏字段。 – Leigh

+0

这是正确的,我仍然可以隐藏所有的值。 –

+0

我上面的答案不是我实际提供的答案。这个答案不回答这个问题。请忽略它。 –