in reply to creating an html table based on db values

This will get you started. I am using the Perl DBI to access a mySQL database, so your results may vary.

This program only asks for 3 columns to display, via check boxes. These settings are not saved to a user in another database table - I left that work for you.

Hope this helps,
Jeff

################################################# # 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 "<table border=1>"; print "<tr>"; foreach (@cols) { print "<th>$_</th>"; } print "</tr>"; # print the rows of returned data foreach my $row (@$stmt) { print "<tr>"; print map { $_ = ($_ eq '') ? "<td>\&nbsp\;</td>" : "<td>$_</td>" } @{$row}; print "</tr>"; } print "</table>"; $conn->disconnect; } else { &printForm; } sub printForm() { print <<_FORM_; <HR> @{[startform('POST',script_name)]} <P> Selct Columns: @{[checkbox_group(-name=>'cols', -values= ['A','B','C'])]}<P> @{[submit('Get Data')]}<P> @{[endform]}<P> <HR> _FORM_ }

Replies are listed 'Best First'.
RE: Re: creating an html table based on db values
by Fastolfe (Vicar) on Sep 21, 2000 at 17:36 UTC
    Be warned that this code is potentially insecure. A malicious user could tamper with the 'cols' form variable and inject their own SQL into your database call(s).