Skip to main content
Participant
November 18, 2009
Answered

Looping an array

  • November 18, 2009
  • 1 reply
  • 377 views

I'm trying to loop an array inside another loop and am running into an issue.  The problem is that the inside loop works but it is looping the outisde loop x number of times

This is where the code is so far.  Any ideas what i'm doing wrong?

<cfset myList = ValueList(AccountProducts.PID)>
<cfset myList = listtoarray(myList)>

<CFLOOP QUERY="GetProductList">
    <CFLOOP index="VPU" from="1" to="#ArrayLen(myList)#">
       
        <CFIF PID EQ #myList[VPU]#>
            <CFINPUT NAME="SKUName" TYPE="checkbox" VALUE="#PID#" CHECKED="Yes" />#SKUName# <br />
        <CFELSE>
            <CFINPUT NAME="SKUName" TYPE="checkbox" VALUE="#PID#" CHECKED="NO" />#SKUName# <br />
        </CFIF>     
    </CFLOOP>       
</CFLOOP>

so an example of what it is doing now is

Product 1
Product 1
Product 1
Product 2
Product 2

Product 2

Product 3

Product 3

Product 3

    This topic has been closed for replies.
    Correct answer ilssac

    Coder76 wrote:

    I'm trying to loop an array inside another loop and am running into an issue.  The problem is that the inside loop works but it is looping the outisde loop x number of times

    Well you are getting exactly what you have coded.

    Your output is inside the inner loop, so it is going to output something every time.  The fix that is most similar to what you have already coded would be to set a flag inside the inner loop and do your output in the outer loop.

    <CFLOOP QUERY="GetProductList">
        <cfset checked = "NO">
        <CFLOOP index="VPU" from="1" to="#ArrayLen(myList)#">
            <CFIF PID EQ myList[VPU]>
                <cfset checked = "YES">
            </CFIF>     
        </CFLOOP>  
        <CFINPUT NAME="SKUName" TYPE="checkbox" VALUE="#PID#" CHECKED="#checked#" />#SKUName# <br />
    </CFLOOP>

    But there are other ways to do this that would involve less lines of code using some of the list functions.

    http://livedocs.adobe.com/coldfusion/8/htmldocs/functions-pt0_13.html

    I think this would produce the same output.

    <cfset myList = ValueList(AccountProducts.PID)>
    <CFLOOP QUERY="GetProductList">
        <CFINPUT NAME="SKUName" TYPE="checkbox" VALUE="#PID#" CHECKED="#yesNoFormat(listFind(myList,PID))#" />#SKUName# <br />
    </CFLOOP>

    1 reply

    ilssac
    ilssacCorrect answer
    Inspiring
    November 18, 2009

    Coder76 wrote:

    I'm trying to loop an array inside another loop and am running into an issue.  The problem is that the inside loop works but it is looping the outisde loop x number of times

    Well you are getting exactly what you have coded.

    Your output is inside the inner loop, so it is going to output something every time.  The fix that is most similar to what you have already coded would be to set a flag inside the inner loop and do your output in the outer loop.

    <CFLOOP QUERY="GetProductList">
        <cfset checked = "NO">
        <CFLOOP index="VPU" from="1" to="#ArrayLen(myList)#">
            <CFIF PID EQ myList[VPU]>
                <cfset checked = "YES">
            </CFIF>     
        </CFLOOP>  
        <CFINPUT NAME="SKUName" TYPE="checkbox" VALUE="#PID#" CHECKED="#checked#" />#SKUName# <br />
    </CFLOOP>

    But there are other ways to do this that would involve less lines of code using some of the list functions.

    http://livedocs.adobe.com/coldfusion/8/htmldocs/functions-pt0_13.html

    I think this would produce the same output.

    <cfset myList = ValueList(AccountProducts.PID)>
    <CFLOOP QUERY="GetProductList">
        <CFINPUT NAME="SKUName" TYPE="checkbox" VALUE="#PID#" CHECKED="#yesNoFormat(listFind(myList,PID))#" />#SKUName# <br />
    </CFLOOP>
    Coder761Author
    Participant
    November 18, 2009

    Thanks ianskinner, both versions worked and the shorter version is a lot cleaner looking.