Converting PHP foreach loop to ColdFusion -


I have a PHP code which is using an advanced loop on the loop and is making a query. I am trying to convert it to coldfusion, but I'm losing, can i show any way how this can be done?

  if ($ deps) {$ num = count ($ deps); $ I = 0; $ Addon = "AND" (foreach ($ deps as $ dep) {$ addon. = "DEPARTMENT_ID = '{$ dep [' dep ']}'". ($ Num! = ++ $ i? 'OR': '');} $ Addon. = ")"; } Else {// has not been assigned any department, so they can not add any tickets $ addon = "AND (DEPARTMENT_ID = '- 1')"; }  

translate directly to CPML: to compile and run it with some changes as required:

  & lt; Cfscript & gt; If (defended ('$ deps')) {$ num = ArrayLen ($ deps); $ I = 0; $ Addon = "AND" ($ dep dep $) for {$ addon & amp; = "DEPARTMENT_ID = '# $ dep [' dep '] #'" & amp; ($ Num NEQ ++ $ i? 'Or': '');} $ Addon & amp; = ")"; } Else {// has not been assigned any department, so they can not add any tickets $ addon = "AND (DEPARTMENT_ID = '- 1')"; } & Lt; / Cfscript & gt;  

This is showing a screenshot of a variation that has changed:

are:

  • Need to check for non-empty values.
  • Instead of
  • foreach (y as y) for translate (code in x)
  • Instead of and and and & nbsp; = . and . =
  • Uses PHP {braces} , when a bracket notation is inside the Y string - for a single variable it is directly $ Varname could be and it would be evaluated. In CFL you use #hashes # to enter the values ​​of the variable in the string.

Note that CF will you! = in the script, but NEQ should be used in the tag-based code (cfif / cfset).

Also keep in mind that when PHP needs to be started to start with $ sign in, CFML does not care in any way, so they No need to remove strictly (even if they are ugly noise).


A similar concept has been made here in simple ways:

  & lt; Cfscript & gt; If (not isDefined ('deps')) deps [1] = {dep = -1}; Addon = []; Addon = "AND" (#ArrayToList (addon, 'OR') #) '; & lt; / cfscript & gt;  

And here's an example of how the same code can work with cfqueryparam instead:

   if (defined 'Dps')) ArrayAppend (depIds, dep.dep);} {depIds = [-1];} & lt; / cfscript & gt; & lt; cfquery & quot; dep depds) for depIds = []; Gt; ... & lt; cfif ArrayLen (depIds) & gt; and DEPARTMENT_ID IN (& lt; cfqueryparam list value = # arreologist (dpiids) # cfsqltype = "cf_sql_integer" / & gt;)  ; & Lt; / cfquery & gt;  

(Proper code is definitely a better variable name And will use the appropriate scoping.)


Comments