Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How can I display DBM entries in an HTML table?

by bladx (Chaplain)
on Dec 29, 2001 at 02:56 UTC ( #135007=perlquestion: print w/ replies, xml ) Need Help??
bladx has asked for the wisdom of the Perl Monks concerning the following question:

Hi ... it's been a while since I have frequented the monastery ^^; but I have finally had time to work on a new project. I am creating a simple news database program that saves to a DBM style database, and can add entries into it, using hashes. I am also using HTML::Template for the first real time to seperate the code from the HTML. My question is, how can I have this code snippet display all of the database entries in a table? Thanks for any help!
note:I have not added the file lock yet, but that will be added before I actually use this code in a bigger project later. I am sure that there is a lot of the program that could be much more efficient than it is, if so, please let me know, so I can learn and fix it! Thanks again.

Here is the first file, the actual code file (news.pl):
#!/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 = (); }


And here is the HTML Template file for the code (news.tmpl):
<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>


That should be all you need to check this program. By the way, this is not a homework question, this is a question that I would like to figure out how to do this sort of program correctly, because last summer I created a web framework news type of thing, but it sucked, and I am hoping this will be better overall than my last project. The old project's site page is at www.hashes.f2s.com ... but it is terrible ^_- so don't see it.

Andy Summers

Edit Petruchio Sat Dec 29 03:17:53 UTC 2001 - Added READMORE tag.

Comment on How can I display DBM entries in an HTML table?
Select or Download Code
Re: How can I display DBM entries in an HTML table?
by jepri (Parson) on Dec 29, 2001 at 08:47 UTC
    Aaah! Evil! Where's your use strict?! If you had one there you'd see that @field_data is being created in one while loop and then passed to a function from a different context. Bad! Doing things like that makes it hard for us to read, which may explain your lack of responses.

    I like they way I can recognise the comments from the HTML::Template examples ;) I spent a lot of time copying out that example and modifying it too.

    Without claiming I know much better, I would probably shorten this bit a little:

    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); }

    can be written as (untested):

    my @newfields = (); foreach $field (@fields){ get_values(); my %nf_row_data; $nf_row_data{FIELD} = $_; push(@field_data, \%nf_row_data); }

    But why do you have get_values() there at all? You don't need it, because you have already used @keys and @values, right?

    Also it is bad practise to have get_values() modify a global variable. Pass @keys and @values out of the subroutine like this:  return \@keys, \@values

    Put use strict; at the top of your program, fix all the errors, then post again.

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

Re: How can I display DBM entries in an HTML table?
by chromatic (Archbishop) on Dec 30, 2001 at 08:17 UTC
    This snippet:
    while (($key,$value) = each %hash) { push(@keys, $key); push(@values, $value); }
    can be replaced with the built-in keys and values functions.

    I second the recommendation to enable strict and to get rid of your global variables. It's also my opinion that you're really overthinking things -- does HTML::Template provide a means to iterate over hash keys? If so, I'd just do that, rather than converting from hash to array of hashrefs.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://135007]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2014-12-26 15:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls