################################################# # Comments: # This CGI program iniatally displays an HTML # form with 3 check boxes. When the user presses # the SUBMIT button, the program is called again, # this time the buttons are parsed if any one of # them were selected. The corresponding columns # of a database table are then retrieved and # displayed inside an HTML table. ################################################# use strict; use DBI; use CGI qw(:standard); print header; # you can replace this predicate with code that # looks for a text box param named 'user' # once you get the value, you can lookup their # setting in the appropriate table - I suggest # just simply saving a comma joined string of # the column names, such as 'A, B, C' - unless # you are against breaking the 1st normal rule if (my @cols = param('cols')) { &printForm; # substitute your host, user, and password my ($host,$user,$pass) = qw(host user pass); my $conn = DBI->connect("DBI:mysql:mysql:$host", $user, $pass) or print "$0: Can't connect to mysql\n"; # construct the SQL code to select only those rows user selected my $sql = "SELECT " . join(', ', @cols) . " FROM foo.bar"; my $stmt = $conn->selectall_arrayref($sql) or print "Query Failed: ", $conn->errstr, "\n"; # print table beginning tags and column names row print ""; print ""; foreach (@cols) { print ""; } print ""; # print the rows of returned data foreach my $row (@$stmt) { print ""; print map { $_ = ($_ eq '') ? "" : "" } @{$row}; print ""; } print "
$_
\ \;$_
"; $conn->disconnect; } else { &printForm; } sub printForm() { print <<_FORM_;
@{[startform('POST',script_name)]}

Selct Columns: @{[checkbox_group(-name=>'cols', -values= ['A','B','C'])]}

@{[submit('Get Data')]}

@{[endform]}


_FORM_ }