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