[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] Millionth SQL -> HTML table script



On Aug 17, 2005, at 9:52 AM, Kenneth Thompson wrote:
An addition nicety might be to add the column names as headers-
especially if you don't know what the column names are when you start
(like select * from blah):

Ya. Good patch. -grin-

The reason for <td><b> instead of actual <th> is so that you could
potentially work in a 'print the columns names every x number of rows'
if you were going to dump a ton of rows.

Why couldn't you do that w/ <th>'s ?

I often code that something like

   print_header_row() unless ($rowcount % 30);

sub print_SQL_and_results {
   my ($strsql) = @_;

   print "<pre>$strsql</pre>\n";
   print "<table>\n";
   my $sth = $dbh->prepare($strsql);
   $sth->execute;
   my @row;

You don't use @row anywhere...?

   my $bHeadersPrinted;
   while (my $haref = $sth->fetchrow_hashref()) {
     my %hash = \$haref;

- Did you mean this?
  %hash = %$haref;

- You don't use %hash anywhere, so I'm not sure why you pulled it.

     if (!$bHeadersPrinted) {
       print "  <tr>";
       foreach my $attr (keys %$haref) {

I'd call that $key or $column.

         print "    <td><B>".$attr."</B></td>";
       }
       print "  </tr>";
       $bHeadersPrinted = "Yuppers";
     }
     print "  <tr>";
     foreach my $attr (sort keys %$haref) {
       print "    <td>".$haref->{$attr}."</td>";
     }

You're getting it! Woo-hoo! Congrats!

Before:
       print "    <td>".$haref->{$attr}."</td>";
After:
       print "    <td>$$haref{$attr}</td>";

(That's one nice thing about avoiding -> hash access syntax: you can do $$hash{$key} straight in your double quotes.)

     print "  </tr>";

You might want to throw a \n in there too in case you want to view HTML source some day.

   }
   $sth->finish;
   print "</table>";
}

Good work!

$0.02,

j