CSN
2005-Dec-13 23:20 UTC
Splitting records into multiple columns (can this code be improved?)
I have an array of categories, and I wanted them split up into two columns in tables: category item_count | category item_count category item_count | category item_count category item_count | category item_count category item_count | category item_count ... Here''s the template code: <table> <tr valign="top"> <td width="50%"> <table width="100%"> <% table_break=(@categories.length.to_f/2).ceil %> <% i=1 %> <% for category in @categories %> <tr> <td nowrap><%= link_to h(category.name), {:controller=>''categories'', :action=>''list'', :category=>category.key} %></td> <td align="right"><%= category.item_count %></td> </tr> <% if i==table_break %></table></td><td width="50%" nowrap><table width="100%"><% end %> <% i+=1 %> <% end %> </table> </td> </tr> </table> Anybody have a simpler way? :) If it was possible to do ''for index category in @categories'' (or in its .each incantation), that''d eliminate the i var, but I don''t see anything like that in Ruby''s docs. I also had ''table_break=(@categories.length/2).ceil'', but apparently Ruby truncates (@categories.length/2)? thanks csn __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com