From: junderdown@... (Jason Underdown)
Date: 2002-08-13T07:49:04+09:00
Subject: Building tables dynamically with CGI class
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 }
}
}
}
}
}
}
|