derekash913 has asked for the wisdom of the Perl Monks concerning the following question:

I am working on an application that will act as a phone directory. One option a user will have is to display all phone numbers in the directory. I am a beginner with perl and I am unsure how to display the contents of a DB in an HTML form? Since this is a phone directory, when this particular run mode is called, I would like to preserve the records being returned and ordered by last name. Do I have to place the rows into an array or a hash before they are displayed in the form? Not sure...here's what I have so far:
sub displayphone { #Retrieve current state of the object my $self = shift; #Prepare DB by establishing DB handle and statement handle my $dbh = $self->param('dbh'); my $sth = $dbh->prepare('Select * FROM directory ORDER BY last_nam +e'); #Execute MySQL $sth->execute; #Create an array to hold the values returned from the DB before th +ey are displayed in a form my @results; foreach ( ) }
  • Comment on Beginner question about displaying database contents using HTML Template
  • Download Code

Replies are listed 'Best First'.
Re: Beginner question about displaying database contents using HTML Template
by bobf (Monsignor) on Jul 19, 2006 at 20:23 UTC

    To continue jZed's example, you could assign the $aoh to a template loop (see the docs for HTML::Template):

    $template->param(MY_LOOP => $aoh);
    and then create a template that looks something like this:
    <TMPL_LOOP NAME="MY_LOOP"> <td><TMPL_VAR NAME=FNAME></td> <td><TMPL_VAR NAME=LNAME></td> <td><TMPL_VAR NAME=PHONENUM></td> </TMPL_LOOP>
    Each record in the $aoh will be output using the template structure in the loop (assuming the keys in the hash for each record include "FNAME", "LNAME", AND "PHONENUM").

    HTML::Template is pretty slick, but you might need to experiment a bit to get the hang of it. Give it a shot and if you get stuck feel free to come back and ask more specific questions.

      Here's where I am at now: I think I am still unsure about how to preserve the ORDER BY once I display the results of the template. Any advice????????
      sub displayphone { #Retrieve current state of the object my $self = shift; #Shortcut to return a reference to an array of hash elements my %attr = ( dbi_fetchall_arrayref_attr => {}); #Preparing the template and substitute the values my $template = HTML::Template->new(filename => 'displayphone.tmpl' +); $template->param(ROWS => $dbh->selectall_arrayref("SELECT * FROM d +irectory ORDER BY lastname", \%attr, $value),); #Display results return $template->output; }
        If you retrieve your results into an arrayref (e.g. with selectall_arrayref), the rows in the array will be in the order returned by the SQL query, in this case ordered by lastname. If you use TMPL_LOOP to display the arrayref, it will display the results in the order they occur in the arrayref. What's the problem?
Re: Beginner question about displaying database contents using HTML Template
by jZed (Prior) on Jul 19, 2006 at 19:39 UTC
    HTML::Template can handle an AoH (array of hash references) which can easily be returned from a DBI query like this:
    my $aoh = $dbh->selectall_arrayref($sql,{Slice=>{}});