Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Looping an array

New Here ,
Nov 18, 2009 Nov 18, 2009

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

326
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Valorous Hero , Nov 18, 2009 Nov 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">
    <
...
Translate
Valorous Hero ,
Nov 18, 2009 Nov 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>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 18, 2009 Nov 18, 2009
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources