5 Replies Latest reply on May 16, 2008 8:15 AM by Newsgroup_User

    CFLOOP Help

    bfinelsen
      I have a simple loop

      <cfloop list="#prodDetails.matchProds#" index="matching" delimiters=",">
      <cfoutput>
      <a href="/catalog/?ProductID=#matching#&Category=#URL.category#"> #matching#</a><br />
      </cfoutput>
      </cfloop>

      That currents displays results as:

      Value 1
      Value 2
      Value 3
      etc...

      I am trying to figure out how to make the out appear as such:

      Value 1 Value 2
      Value 3 Value 4
      Value 5 etc...

      Any help here would be much appreciated. Regards.
        • 1. Re: CFLOOP Help
          Level 7
          bfinelsen wrote:
          >
          > Value 1 Value 2
          > Value 3 Value 4
          > Value 5 etc...
          >
          > Any help here would be much appreciated. Regards.
          >

          You need a conditional <cfif> block around the <br/> tag.

          The condition for this if block needs to be true when you want the <br/>
          displayed and when it is not.

          If you want to and only two elements between each <br/> tag, you could
          get by with a boolean that is flipped back and forth.

          If you want more flexibility where you can have different numbers of
          element between the <br/> tags, use a counter and the MOD operator.

          Is that enough help?

          • 2. Re: CFLOOP Help
            paross1 Level 2
            Something like this?

            <cfloop list="#prodDetails.matchProds#" index="matching" delimiters=",">
            <cfoutput>
            <cfif matching MOD 2 EQ 0>
            <a href="/catalog/?ProductID=#matching#&Category=#URL.category#"> #matching#</a><br />
            <cfelse>
            <a href="/catalog/?ProductID=#matching#&Category=#URL.category#"> #matching#</a>&nbsp
            </cfif>
            </cfoutput>
            </cfloop>

            Phil
            • 3. Re: CFLOOP Help
              bfinelsen Level 1
              Thank you! Using your suggestion, I have used a boolean type of syntax to make it work perfectly. Many thanks.
              • 4. Re: CFLOOP Help
                Level 7
                paross1 wrote:
                > Something like this?
                >
                > <cfloop list="#prodDetails.matchProds#" index="matching" delimiters=",">
                > <cfoutput>
                > <cfif matching MOD 2 EQ 0>
                > <a href="/catalog/?ProductID=#matching#&Category=#URL.category#">
                > #matching#</a><br />
                > <cfelse>
                > <a href="/catalog/?ProductID=#matching#&Category=#URL.category#">
                > #matching#</a>&nbsp
                > </cfif>
                > </cfoutput>
                > </cfloop>
                >
                > Phil
                >

                Somewhat, except that I would just put the <cfif> block around the <br/>
                tag, not the entire <a...> tag that is the same in both clauses.

                But <cfif matching MOD 2> does not work in this case. Since this is a
                list loop 'matching' is going to be a string containing each element in
                the list.

                One would need to either create an independent counter to be used in the
                MOD clause or use a listFind() function to get the numeric position from
                the list from which the string in 'matching' came.
                • 5. Re: CFLOOP Help
                  Dan Bracuk Level 5
                  To make that code more efficient, put the loop inside the cfoutput instead of the way you have it now.