Saturday, 29 June 2013

Coldfusion: How to iterate two lists in the same loop

Question :

 I have to iterate two lists at the same time in a loop.Suppose,I have a list as follows -
"d,m,c,k|delhi,mumbai,chennai,kolkata" and I need to create a drop down having value from first half of the list i.e."d,m,c,k" and display string will be the values from "delhi,mumbai,chennai,kolkata".How to we acheive this in the coldfusion?

Code :

<cfoutput>
  <cfset list = "d,m,c,k|delhi,mumbai,chennai,kolkata" />
  <cfset arrTemp = listToArray(list,"|") />
  <cfset list1 = arrTemp[1] />
  <cfset list2 = arrTemp[2] />
  <select name = "city">
  <cfloop from="1" to="#ListLen(list1)#" index="i">
    <option value="#ListGetAt(list1,i)#">
      #ListGetAt(list2,i)#
    </option>
  </cfloop>
  </select>  
</cfoutput>

Output :



After looking at the code one can easily iterate on the third or more no. of lists.Hope this helps !!!