I figured out how to build tables dynamically using methods in the CGI
class, but I can’t figure out a way to add a header other than the
method I use below. In the code below, I simply put the header row
into the array of rows generated by successive fetches from the
database.
The problem with the method I use below is that it forces me to use
tags for the header columns when I would like to use | tags.
Any suggestions?
#!/usr/bin/env ruby
require 'dbi’
require ‘cgi’
connect to the database
dbh = DBI.connect(‘DBI:Mysql:test’,’’,’’)
cgi = CGI.new(“html4”)
sth = dbh.prepare(‘select id, fname, lname from contact’)
sth.execute
rows = [[“ID”,“First”,“Last”]]
while row = sth.fetch_array() do
rows.push(row)
end
cgi.out {
cgi.head { cgi.title{“Name”} } +
cgi.body {
cgi.table(‘border’=>‘1’) {
rows.collect { |row|
cgi.tr {
row.collect { |item|
cgi.td { item }
}
}
}
}
}
}
|
“T.M. Sommers” tms2@mail.ptd.net wrote in message news:3D5875BF.72164C6E@mail.ptd.net…
Jason Underdown wrote:
I figured out how to build tables dynamically using methods in the CGI
class, but I can’t figure out a way to add a header other than the
method I use below. In the code below, I simply put the header row
into the array of rows generated by successive fetches from the
database.
The problem with the method I use below is that it forces me to use
tags for the header columns when I would like to use tags.
Any suggestions?
Did you try CGI#th?
I am fully aware of CGI#th. Maybe I didn’t explain myself well
enough. What I want to be able to do is something like the following
code (which doesn’t work):
cgi.table(‘border’=>‘1’) {
cgi.tr { cgi.th{“ID”} + cgi.th{“First”} + cgi.th{“Last”} } +
rows.collect { |row|
cgi.tr {
row.collect { |item|
cgi.td { item }
}
}
}
}
My previous code posting works, but notice that since the header row
gets generated by the same loop which creates the table body, the
CGI#td method is used to generate the header cells instead of CGI#th.
junderdown@msn.com (Jason Underdown) writes:
I am fully aware of CGI#th. Maybe I didn’t explain myself well
enough. What I want to be able to do is something like the following
code (which doesn’t work):
cgi.table(‘border’=>‘1’) {
cgi.tr { cgi.th{“ID”} + cgi.th{“First”} + cgi.th{“Last”} } +
rows.collect { |row|
cgi.tr {
row.collect { |item|
cgi.td { item }
}
}
}
}
My previous code posting works, but notice that since the header row
gets generated by the same loop which creates the table body, the
CGI#td method is used to generate the header cells instead of CGI#th.
Change
row.collect { |item|
cgi.td { item }
}
to
row.collect { |item|
cgi.td { item }
}.join("\n")
because the value of the block needs to be a single string.
Jim
···
–
Jim Menard, jimm@io.com, http://www.io.com/~jimm/
“Power corrupts. Absolute power is kinda neat.” – Jeffrey Kaplan in r.h.o.d
Thanks for your help Jim. The following code does exactly what I want:
#!/usr/bin/ruby
require ‘cgi’
cgi = CGI.new(“html4Tr”)
table = [
[‘00’, ‘01’, ‘02’, ‘03’],
[‘10’, ‘11’, ‘12’, ‘13’],
[‘20’, ‘21’, ‘22’, ‘22’],
[‘30’, ‘31’, ‘32’, ‘33’]
]
cgi.out{
cgi.body{
cgi.table{
cgi.tr{cgi.th{“One”} +
cgi.th{“Two”} +
cgi.th{“Three”} +
cgi.th{“Four”}} +
table.collect{|row|
cgi.tr{
row.collect{|item|
cgi.td{ item }
}.join
}
}.join
}
}
}
Jim Menard jimm@io.com wrote in message news:wsqadnq4kqv.fsf@io.com…
···
junderdown@msn.com (Jason Underdown) writes:
I am fully aware of CGI#th. Maybe I didn’t explain myself well
enough. What I want to be able to do is something like the following
code (which doesn’t work):
cgi.table(‘border’=>‘1’) {
cgi.tr { cgi.th{“ID”} + cgi.th{“First”} + cgi.th{“Last”} } +
rows.collect { |row|
cgi.tr {
row.collect { |item|
cgi.td { item }
}
}
}
}
My previous code posting works, but notice that since the header row
gets generated by the same loop which creates the table body, the
CGI#td method is used to generate the header cells instead of CGI#th.
Change
row.collect { |item|
cgi.td { item }
}
to
row.collect { |item|
cgi.td { item }
}.join("\n")
because the value of the block needs to be a single string.
Jim