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_ }

In reply to (jeffa) Re: creating an html table based on db values by jeffa
in thread creating an html table based on db values by jmac

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.