Loop through and list a 2 column db table from Tinytds via ruby with column1 values as headings and column2 values as a list -
i've been trying learn basics of loops , i'm getting there i'm struggling brain around following requirement where, example, i've got data database table via tinytds, such key/values hashed array seems(? - beginner here, hope terminology correct!):-
the data such values 1 'column' of db table repeated many times, whilst values of 2nd 'column' unique.
therefore, rather creating table in html output has 2 columns column1 showing same line of text on , on (before next unique string lists on , over), i'd present information unique string values column1 headings , values of column2 listed beneath.
in mind, think i'm trying achieve loop through column1 each unique string, output value, whilst running inner loop list values column2 column1 'string', moving on next unique value of column1,output value, loop through column2 again list values column1 'string2' etc.
hope makes sense i'm struggling know how explain in correct terminology.
thanks.
for example, if 2 columns database table follows:-.
column1
q1
q1
q1
q2
q2
q3
q3
q3
column2
a1
a2
a3
a4
a5
a6
a7
a8
how loop (or whatever best) through able present output such as:-
q1
a1
a2
a3
q2
a4
a5
q3
a6
a7
a8
i can write html side format output, can't figure out ruby side.
for single loop i've picked tiny_tds examples following ruby (the html here test placement now):-
<% narrative.each |question| %> <span><%=question.values[1] %></span> <br/> <% end %>
that allows me list values slot 2 (is correct terminology?) , like:-
<% narrative.each |question| %> <span><%=question.values[0] %></span><span><%=question.values[1] %></span> <br/> <% end %>
to show both side side, i'm wanting more like:-
<h1>question1 goes here</h1> <ul> <li>answer 1</li> <li>answer 2</li> <li>answer 3</li> </ul> <h1>question2 goes here</h1> <ul> <li>answer 4</li> <li>answer 5</li> </ul>
etc.
if want keep simple, way, giving logic, in stead of puts add html tags
a1 = [:q1,:q1,:q1,:q2,:q2,:q3,:q3,:q3] a2 = [:a1,:a2,:a3,:a4,:a5,:a6,:a7,:a8] a3 = a1.zip a2 #=>[[:q1, :a1], [:q1, :a2], [:q1, :a3], [:q2, :a4], [:q2, :a5], [:q3, :a6], [:q3, :a7], [:q3, :a8]] a3.each_with_index |e, i| puts "#{e[0] if e[0] != a3[i-1][0]} #{e[1]}" end => q1 a1 a2 a3 q2 a4 a5 q3 a6 a7 a8
Comments
Post a Comment