jmac has asked for the wisdom of the Perl Monks concerning the following question:

Hi all. I'm trying to create a table based on user preference. Basically, the user has the ability to use checkboxes to specify which fields should be displayed. If the field is checked, the value 'checked' is saved to a database. If a field has a value of 'checked', it should show up as one of the headers in the table. If not, it should not be displayed. I'm confused about the best/easiest way to do this. Can you guys help me out?
  • Comment on creating an html table based on db values

Replies are listed 'Best First'.
(jeffa) Re: creating an html table based on db values
by jeffa (Bishop) on Sep 21, 2000 at 04:34 UTC
    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_ }
      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).
Re: creating an html table based on db values
by ncw (Friar) on Sep 21, 2000 at 02:37 UTC
    Are you talking about a CGI interface to a Database?

    If so you can roll your own with CGI and DBI or if you are really only displaying database tables you might want to look at DBIx::HTMLinterface for some help.

    Alternatively specify your problem a bit more and we'll see if we can be more specific!

Re: creating an html table based on db values
by bubble (Novice) on Sep 21, 2000 at 11:37 UTC
    A HTML-table based on user-input which has it's storage based in a DB. Depends on how you created your database-statement. just capture all cgi->param's then make a select statement which has a WHERE clause in it then you can either use the param's directly ad input into your where clause and finally but most important use "fetchhashref" Thus create an array of hases which via the module HTML::Template you can display. Don't know if you ever used HTML::Template but it would make your project a lot easier. greets
Re: creating an html table based on db values
by jmac (Initiate) on Sep 21, 2000 at 02:47 UTC
    More specific: I want to create an HTML table based on user preference. The table by default is displayed with 8 fields. The user is presented with a set of check boxes that he/she can check to remove or add specific parts of the table. For instance, I want "Field_A" to be part of the table, so i check the field_a checkbox. If I don't want this field to be displayed, i uncheck it. If a checkbox is clicked, 'checked' is saved to a database. I want to be able to display the fields that have the value 'checked' in the database.