Iteration through url request collection variables by dynamic evaluation

我与影子孤独终老i 提交于 2019-12-25 04:14:33

问题


What i am trying todo in coldfusion cfscript is iterate through a request collection of variables and do some evaluation, which i can do easily in PHP, but am running into issue translating to coldfusion cfscript, because it seems i cannot build a dynamic if statement

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 ";
            }

I have tried this, errored out

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 ";}
  }         
}

also tried changing the if statement line to this, same

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

finally i attempted building a string and using that , i knew it wouldnt work but i thought i'd give it a shot, error was could not convert var to boolean

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

Here's the static coldfusion without iteration that i am desiring to be done problematically

  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 ";}                                     
  }

any help would be much obliged


回答1:


As Leigh says, you just need to refer to dynamic columns like this:

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


来源:https://stackoverflow.com/questions/16920589/iteration-through-url-request-collection-variables-by-dynamic-evaluation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!