3 columns

Hi all,

Not really sure what to put in the title so hope people still open this!

What I want to do is produce something like this

Col1 Col2 Col3
data1 data2 data3
data4 data5 data6
data7 data8 data9
data… data… data…
data… data… data…
data-n data-n+1 data-n+2

I can get the data back from my db happily, but I cant for the life of
me work out how to actually display it. Single column is easy, but
anything else has me stumped.

Can anyone help?

Thanks

Jonathan

I’d normally have an iterator that increments each loop and if it’s
divisible by 3 print a closing tr tag and start another table row.
other than that print every value inside a table cell.

Ross

Ross R. wrote:

I’d normally have an iterator that increments each loop and if it’s
divisible by 3 print a closing tr tag and start another table row.
other than that print every value inside a table cell.

Ross

if @results contains ur class objects:

<% @results.each do |obj| -%> <% end -%>
<%= obj.col1 %> <%= obj.col2 %> <%= obj.col3 %>

Yes but that only works if you are doing one table row per db row. I
thought the original question implied that he was iterating through a
DB and every third DB row would drop to another table row.

Ross

Ross R. wrote:

Yes but that only works if you are doing one table row per db row. I
thought the original question implied that he was iterating through a
DB and every third DB row would drop to another table row.

Ross

Something like that?

<% @results.each_with_index do |obj, i| -%> <%= "" if 2 == i.modulo(3) -%> <% end -%>
<%= obj.col1 %> <%= obj.col2 %> <%= obj.col3 %>

Yes that’s what I was thinking…

Thanks to both of you! Saved me hours of head scratching!

What I ended up with is the following,

<% @results.each_with_index do |obj, i| -%> <%= "" if 2 == i.modulo(3) -%> <% end -%>
<%= obj.columnvalue%>

Which gives me the following:

record1.colvalue record2.colvalue record3.colvalue
record4.colvalue record5.colvalue record6.colvalue
record7.colvalue record8.colvalue record9.colvalue

Thanks once again for pushing me in the right direction.

Jonathan

Did you try in_groups_of ?

Check out
http://weblog.rubyonrails.com/2006/3/1/new-in-rails-enumerable-group_by-and-array-in_groups_of
for a short tutorial.

oomtuinstoel wrote:

Did you try in_groups_of ?

Check out
Peak Obsession
for a short tutorial.

Very nice!

Hello Jonathan,

Thanks to both of you! Saved me hours of head scratching!

What I ended up with is the following,

<% @results.each_with_index do |obj, i| -%> <%= "" if 2 == i.modulo(3) -%> <% end -%>
<%= obj.columnvalue%>

You can also use Array#in_groups_of :

<% @results.map(&:columnvalue).in_groups_of(3, fill_with=' ') do |group| -%> <% for col in group -%> <% end -%> <% end -%>
<%= col %>

There will be "

 " code that will pad your table
if the size of @results is not a multiple of 3.

РJean-Fran̤ois.

Ok so I’m a newbie, and I’m confused!

I want to also disply the reulst in columns, nut only two. I begin my
loop with:

<% for @post in @posts %>

<% end %>

How can I get this to display in two colums rather than one?
Thanks

Here is what i got so far.

<% (@post = Post.find(:all)).to_a.in_groups_of(3) do |group| %> <% group.each do |title| %> <% end %> <% end %>
<%= title %>

title = column value in db.

I get in return the right table div’s, but no info only hashes. It looks
like this when render:

So I know the columns are working, but how do I get it to display the
data?