[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

force cfloop to display in columns left to right

Posted on 2011-10-12
15
Medium Priority
?
555 Views
Last Modified: 2012-08-13
hi all,

i've got a cfloop which currently displays the results in a list.

what i'd like is for the data to be displays left to right in three columns like so:

1 2 3
4 5 6
7 8 9

here is the CF code and the css.

<div class="productList">
			<cfloop array="#$.cart.productList().getPageRecords()#" index="local.product">
            <div class="productListEachProd">
				<a href="#local.product.getProductURL()#">
					<dl>
						<dt class="image">#local.product.getImage("s")#</dt>
						<dt class="title">#local.product.getTitle()#</dt>
						<dd class="price">#DollarFormat(local.product.getLivePrice())#</dd>
					</dl>
				</a>
             </div>
			</cfloop>
		</div>

Open in new window



and here is the current very basic css:


.productList {
	border: 1px #000000 solid;
	
	}
	
.productListEachProd {
	border: 2px #FF0000 solid;
	
	}

Open in new window


many thanks
0
Comment
Question by:bede123
  • 7
  • 5
  • 3
15 Comments
 
LVL 28

Assisted Solution

by:strickdd
strickdd earned 1000 total points
ID: 36956363
in a cfif statement check #(intValue MOD 3)# where "intValue" is a variable that is incremented every loop.

If the value is 0, add a line break.
0
 
LVL 1

Author Comment

by:bede123
ID: 36956401
many thanks but i'll need more help than that. i'm not experianced with writing cf. can your show me wha it woudl look like please?

would i wrap the entire cfif round the list? like so:



<cfif #(intValue MOD 3)# where....>


<div class="productList">
<cfloop array="#$.cart.productList().getPageRecords()#" index="local.product">
<div class="productListEachProd">
<a href="#local.product.getProductURL()#">
<dl>
<dt class="image">#local.product.getImage("s")#</dt>
<dt class="title">#local.product.getTitle()#</dt>
<dd class="price">#DollarFormat(local.product.getLivePrice())#</dd>
</dl>
</a>
</div>
</cfloop>
</div> 



</cfif>

Open in new window

0
 
LVL 28

Assisted Solution

by:strickdd
strickdd earned 1000 total points
ID: 36956463
No, the CFIF has to be inside the loop. Here is a similar sample to what you are trying, you just have to modify it to suit your needs: http://www.bennadel.com/blog/665-And-On-The-Seventh-Row-MOD-Created-1-And-It-Was-Good.htm
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:bede123
ID: 36956866
right, ive given this a stab - in the dark, pitch black in fact :-)

<div class="productList">
	<cfloop array="#$.slatwall.productList().getPageRecords()#" index="local.product">
            	<cfif (#local.product# MOD 3) eq 0>
            <br/> 
            	<cfelse>     
            <div class="productListEachProd">
				
<dl>
<dt class="image"><a href="#local.product.getProductURL()#">#local.product.getImage("s")#</a></dt>
<dt class="title">#local.product.getProductName()# </dt>
<dd class="price">#DollarFormat(local.product.getLivePrice())#</dd>
<dt class="more"><a href="#local.product.getProductURL()#">More Info &gt;&gt;</a></dt>
</dl>
				
             </div>
             	</cfif>
	</cfloop>
		</div>

Open in new window



but there is clearly soemthing wrong with my format. can you have a look please?

0
 
LVL 28

Expert Comment

by:strickdd
ID: 36957017
You're getting there... there are two problems. First, you are checking "#local.product# mod 3" which is not an integer counter. You need to add a variable that increments each loop and check that or check the row number of the current product.

The next problem is that you have the code to print product information happening on all rows that are not multiples of three so your output would be like this:

12
45
..

instead of
123
456
...

If the MOD statement is true, you want to brint the <br/> as well as the product info.
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 1000 total points
ID: 36957094
@strickdd's right. Using MOD to add a line break will do the trick.  

     
<cfif (#local.product# MOD 3) eq 0>
            <br/>
    <cfelse>    
       ... code to generate <dl> list
    </cfif>

But that will skipevery 3rd <dl> list. You need to create the <dl> list every time, but add the line break at the end IF it's the 3rd row:

        
<cfloop array="#$.slatwall.productList().getPageRecords()#" index="local.product">

    ... your code to generate the <dl> ...

    <!--- if we've displayed 3 elements it's time to add a line break --->
    <cfif (local.product MOD 3) eq 0>
       <br/> 
    </cfif>

</cfloop>

Open in new window

       

<div class="productListEachProd">

The other potential problem is <div>'s always generate a line break.  So even with the code above, the output won't be in columns unless you use something like float to suppress the automatic line break. Example:

<style>
 .productListEachProd { width: 33%; float: left; }
</style>

Open in new window

0
 
LVL 52

Expert Comment

by:_agx_
ID: 36957102
Oops, took too long.

> you are checking "#local.product# mod 3" which is not an integer

Good catch.  I missed that it was an array loop, not a from/to loop.
0
 
LVL 1

Author Comment

by:bede123
ID: 36957153
ok thanks very much both of you.

i'm understanding (a little bit) and ive inserted the cfif into the correct position.

however now you've got me on:

>You need to add a variable that increments each loop and check that or check the row number of the current product

i just dont know where to begin with that.
0
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 1000 total points
ID: 36957324
All that means is create a simple counter.

       <cfset counter = 1>

Each time you loop, increment the counter by 1.

     <cfloop ...>
           ....
          <cfset counter = counter + 1>
     </cfloop>

      > you are checking "#local.product# mod 3"

Then check the #counter# variable instead of #local.product#

0
 
LVL 1

Author Comment

by:bede123
ID: 36957597
ok great, i'm getting somewhere.

the error i'm getting is:

The value coldfusion.orm.PersistentTemplateProxy cannot be converted to a number.

using this code:

<cfset counter = 1>

<div class="productList">
                  <cfloop array="#$.slatwall.productList().getPageRecords()#" index="local.product">
                      
            <div class="productListEachProd">
                        
                              <dl>
                                    <dt class="image"><a href="#local.product.getProductURL()#">#local.product.getImage("s")#</a></dt>
                                    <dt class="title">#local.product.getProductName()# </dt>
                                    <dd class="price">#DollarFormat(local.product.getLivePrice())#</dd>
                        <dt class="more"><a href="#local.product.getProductURL()#">More Info &gt;&gt;</a></dt>
                              </dl>
                        
             </div>
            <cfset counter = counter +1>
                    <!--- if we've displayed 3 elements it's time to add a line break --->
    <cfif (counter MOD 3) eq 0>
       <br/>
    </cfif>


                  </cfloop>
            </div>


so this just probably means i havent enclosed something correctly or something simple like that. ive tried a few variations of things. can you see whats wrong?

0
 
LVL 52

Expert Comment

by:_agx_
ID: 36957668
Nothing jumps out at me.  What's the line number of the error?  

> <cfset counter = counter +1>

No, you have to increment the counter after the MOD test. Otherwise it'll be off by 1

first loop        : counter == 2
second loop   : counter == 3     ... insert line break too early
third loop       : counter == 4
...
0
 
LVL 1

Author Comment

by:bede123
ID: 36957791
yey, got it to work with:

<cfset counter = 1>
            <div class="productList">
                  <cfloop array="#$.slatwall.productList().getPageRecords()#" index="local.product">
                      
            <div class="productListEachProd">
                        
                              <dl>
                                    <dt class="image"><a href="#local.product.getProductURL()#">#local.product.getImage("s")#</a></dt>
                                    <dt class="title">#local.product.getProductName()# </dt>
                                    <dd class="price">#DollarFormat(local.product.getLivePrice())#</dd>
                        <dt class="more"><a href="#local.product.getProductURL()#">More Info &gt;&gt;</a></dt>
                              </dl>
                        
             </div>
           
                    <!--- if we've displayed 3 elements it's time to add a line break --->
    <cfif (counter MOD 3) eq 0>
       <br/>
    </cfif>
<cfset counter = counter +1>

                  </cfloop>
            </div>
       


just needs to tidy it up a bit and we're all sorted.

thanks very much for your help guys.
0
 
LVL 1

Author Comment

by:bede123
ID: 36957905
oh but wait, i just added a 4th product to the db but only 3 are displaying. do you think that might be related to my code or something else?

0
 
LVL 52

Expert Comment

by:_agx_
ID: 36957931
Definitely something else.  Maybe ORM related.
0
 
LVL 1

Author Comment

by:bede123
ID: 36957940
ok thats cool. at least i know to look somewhere else and not go touching the code we've just done. thanks again.

hopefully your guys agree a 50/50 split.

many thanks

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question