【问题标题】:Iteration through url request collection variables by dynamic evaluation通过动态评估迭代url请求集合变量
【发布时间】:2013-06-05 00:36:58
【问题描述】:

我在 Coldfusion cfscript 中尝试做的是遍历请求的变量集合并进行一些评估,这在 PHP 中可以轻松完成,但在转换为 Coldfusion cfscript 时遇到了问题,因为我似乎无法构建动态if 语句

PHP

for ( $i=0 ; $i<count($aColumns) ; $i++ )
    {
        if ( $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
        {
            //If there was no where clause
            if ( $sWhere == "" )
            {
                $sWhere = "WHERE ";
            }
            else
            {
                $sWhere .= " AND ";
            }

我试过了,出错了

for (i=1; i<= iColumnsLen; i++) {   
  if (rc.bSearchable_&i EQ true and rc.sSearch&i NEQ '') {
            if ( sWhere EQ "" )
            { sWhere = " WHERE "; }
            else
            { sWhere &= " AND ";}
  }         
}

也试过把 if 语句改成这样,一样

if (rc.bSearchable_+i EQ true and rc.sSearch+i NEQ '') {

最后我尝试构建一个字符串并使用它,我知道它不起作用但我想我会试一试,错误是无法将 var 转换为布尔值

for (i=1; i<= iColumnsLen; i++) {
 var iterator = "rc.bSearchable_"&i&" EQ true and rc.sSearch_"&i&" NEQ ''";
  if (#iterator#) {

这是我希望有问题地完成的没有迭代的静态冷融合

  if (rc.bSearchable_1 EQ true and rc.sSearch_1 NEQ '') {
            if ( sWhere EQ "" )
            { sWhere = " WHERE "; }
            else
            { sWhere &= " AND ";}
  }         
  if (rc.bSearchable_2 EQ true and rc.sSearch_2 NEQ '') {
            if ( sWhere EQ "" )
            { sWhere = " WHERE "; }
            else
            { sWhere &= " AND ";}
  } 
  if (rc.bSearchable_3 EQ true and rc.sSearch_3 NEQ '') {
            if ( sWhere EQ "" )
            { sWhere = " WHERE "; }
            else
            { sWhere &= " AND ";}                                     
  }

任何帮助都会非常感激

【问题讨论】:

  • 我试过了,出错了。错误信息是什么?仅供参考:URL 范围是一个结构。您可以使用关联数组表示法动态访问这些值:即url["someFixedName"&amp; counterVariable ]
  • .. 这同样适用于任何结构。

标签: coldfusion cfml coldbox


【解决方案1】:

正如 Leigh 所说,您只需要像这样引用动态列:

rc["bSearchable_" & i] 
rc["sSearch_" & i]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    相关资源
    最近更新 更多