Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

poj's scratchpad

by poj (Prior)
on Jun 12, 2013 at 17:16 UTC ( #1038520=scratchpad: print w/replies, xml ) Need Help??

#!/usr/bin/perl # 2017-08-04 version 2 use strict; use DBI; use HTML::Template; my $dir = 'c:/temp'; my @headings = qw(Football Baseball); my $t = join '',<DATA>; my $template = HTML::Template->new( scalarref => \$t ); my $sql = q! SELECT team as TEAM, url as URL FROM sports WHERE sport = ? !; my ($dbname,$user,$passwd); my $dbh = DBI->connect("dbi:Oracle:$dbname", $user, $passwd, {RaiseError => 1}) or die "Oracle Connect Failed: ", DBI->errstr; my $sth = $dbh->prepare($sql); # create 1 page for each heading for my $tab (@headings){ print "Generating html for $tab\n"; set_params($template,$tab); open HTM ,'>', "$dir/$tab.html" or die $!; print HTM $template->output; close HTM; } sub set_params { my ($template,$tab) = @_; $sth->execute($tab); my @rows; while (my @f = $sth->fetchrow_array) { push @rows, { TEAM => $f[0], URL => $f[1], }; } my @navbar; for (@headings){ if ($_ eq $tab){ push @navbar,{ LINK=>qq!<td class="tab_actv"> $_ </td>!}; } else { push @navbar,{ LINK=>qq!<td class="tab"><a href="$_.html"> $_ </ +a></td>!}; } } $template->param(TITLE=>$tab ); $template->param(NAVBAR=>\@navbar, ROWS=>\@rows ); } __DATA__ <html> <title><TMPL_VAR name=TITLE></title> <style type="text/css"> td.tab { padding: 2 12 2 12; width: 80; background-color: #FA +FAD2 } td.tab_actv { padding: 2 12 2 12; width: 80; background-color: #C0 +D4E6 } </style> <body> <table><tr> <TMPL_LOOP NAME=NAVBAR> <TMPL_VAR NAME=LINK> </TMPL_LOOP> </tr></table> <table border="1" cellpadding="5" cellspacing="0"> <TMPL_LOOP NAME=ROWS> <tr> <td><TMPL_VAR NAME=TEAM></td> <td><A HREF="<TMPL_VAR NAME=URL>" target="_blank"><TMPL_VAR NAME=UR +L></A></td> </tr> </TMPL_LOOP> </table> </body> </html>
-------------------------------------------------------
#!/usr/bin/perl use strict; use DBI; use HTML::Template; my @headings = qw(Football Baseball); my $template = join '',<DATA>; for (@headings){ print "Generating html for $_\n"; generate_html($_); } sub generate_html { my ($tab) = @_; my $heading = get_heading($tab); my $sql = q! SELECT team as TEAM, url as URL FROM sports WHERE sport = ? !; my $dbh = DBI->connect("dbi:Oracle:$dbname", $user, $passwd, {RaiseError => 1}) or die "Oracle Connect Failed: ", DBI->errstr; my $sth = $dbh->prepare($sql); $sth->execute($tab); my @rows; while (my @f = $sth->fetchrow_array) { push @rows, { TEAM => $f[0], URL => $f[1], }; } my $template = HTML::Template->new( scalarref => \$template ); $template->param(TITLE=>"ENV Spreadsheet" ); $template->param(HEADINGS=>$heading, ROWS=>\@rows ); open HTM ,'>',"c:/temp/$tab.html" or die $!; print HTM $template->output; close HTM; } sub get_heading { my $tab = shift; my @navbar; for (@headings){ if ($_ eq $tab){ push @navbar,qq!<span class="tab_actv"> $_ </span>!; } else { push @navbar,qq!<span class="tab"><a href="$_.html"> $_ </a></sp +an>!; } } return join ' &nbsp; ',@navbar; } __DATA__ <html> <title><TMPL_VAR name=TITLE></title> <style type="text/css"> .tab { padding: 2 12 2 12; width: 80; background-color: #FAFA +D2 } .tab_actv { padding: 2 12 2 12; width: 80; background-color: #C0D4 +E6 } </style> <body> <TMPL_VAR NAME=HEADINGS> <table border="1" cellpadding="5" cellspacing="0"> <TMPL_LOOP NAME=ROWS> <tr> <td><TMPL_VAR NAME=TEAM></td> <td><A HREF="<TMPL_VAR NAME=URL>" target="_blank"><TMPL_VAR NAME=UR +L></A></td> </tr> </TMPL_LOOP> </table> </body> </html>
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2017-12-14 23:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (415 votes). Check out past polls.

    Notices?