#!/usr/bin/perl -w use HTML::Template; use CGI qw (:standard); use CGI::Carp qw(fatalsToBrowser); use DB_File; @fields = ("title","date","author","news"); $q = new CGI; print $q->header; my $template = HTML::Template->new(filename => 'news.tmpl'); $filename="database.db"; tie(%hash, "DB_File", $filename) or die "Can't open $filename: $!"; #add(); my @loop_data = (); # initialize an array to hold your loop my @keys = (); my @values = (); get_values(); sub get_values { while (($key,$value) = each %hash) { push(@keys, $key); push(@values, $value); } } while (@keys and @values) { my %row_data; # get a fresh hash for the row data # fill in this row $row_data{KEY} = shift @keys; $row_data{VALUE} = shift @values; # the crucial step - push a reference to this row into the loop! push(@loop_data, \%row_data); } my @newfields = (); foreach $field (@fields){ push(@newfields, $field); } while (@newfields) { get_values(); my %nf_row_data; $nf_row_data{FIELD} = shift @newfields; push(@field_data, \%nf_row_data); } #delete(); untie %hash; $template->param( SHOW_ALL_LOOP => \@loop_data, FIELD_LOOP => \@field_data); print $template->output; sub add { $key = time(); $record = $key; foreach $field(@fields){ $record .= "\|$field"; } $hash{$key} = "$record"; } sub delete { %hash = (); }
<html> <head><title>news.tmpl</title></head> <body bgcolor=#eeeeee> <center> <TMPL_LOOP NAME="SHOW_ALL_LOOP"> <table width=400 border=1 cellspacing=0 cellpadding=0> <tr><td valign=top bgcolor=#dddddd> <b>Key</b>:<TMPL_VAR NAME="KEY"><br> <b>Value</b>:<TMPL_VAR NAME="VALUE"><br><br> </td></tr> </table><br> </TMPL_LOOP> <table border=1 cellspacing=0 cellpadding=0> <tr> <TMPL_LOOP NAME="FIELD_LOOP"> <td><TMPL_VAR NAME="FIELD"></td> </TMPL_LOOP> </tr> </table> </center> </body> </html>
Edit Petruchio Sat Dec 29 03:17:53 UTC 2001 - Added READMORE tag.
In reply to How can I display DBM entries in an HTML table? by bladx
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |