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

Can this be rewritten any better?
I somehow dont like the way i have it and i think it can be rewritten.
What if i had 50 rows?
Also i dont like the colspan and alignment but i dont know how to set css handle this.
if( param('Εμφάνιση Guestbook!') ) { $sth = $dbh->prepare( 'SELECT * FROM guestbook' ); $sth->execute; while( $row = $sth->fetchrow_hashref ) { print table( {class=>'user_form'}, Tr( td( {-width=>'25%'}, {class=>'name'}, $row->{ +name} ), td( {-width=>'50%'}, {class=>'email'}, $row->{ +email} ), td( {-width=>'25%'}, {class=>'date'}, $row->{ +date} ) ), Tr( td( {class=>'host'}, "Ευχή τ +ου Ιησού" ), td( {colspan=>2}, {class=>'tip'}, $row->{ +pray} ) ), Tr( td( {class=>'tip'}, "Προσωπ +ική Ορθοδοξοπνευματική Εμπειρία" ), td( {colspan=>2}, {class=>'remark'}, $row->{ +remark} ) ), Tr( td( {colspan=>3}, {class=>'host'}, $row->{ +host} ) ), ), br() x 2; } unless ($sth->rows ) { print span( {class=>'tip'}, "Δεν υπάρχουν νέες εγγραφές, yet!" + ); } }

Replies are listed 'Best First'.
Re: Perl and a little style.
by tlm (Prior) on May 02, 2005 at 13:02 UTC

    Printing out HTML with Perl is inherently ugly; no getting around that. If it really bothers you (and I can see why it would), then use a templating system.

    the lowliest monk

      Yes, I agree.

      Take a look at Template Toolkit

      HTH.

      Walking the road to enlightenment... I found a penguin and a camel on the way.....
      Fancy a yourname@perl.me.uk? Just ask!!!
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Perl and a little style.
by wfsp (Abbot) on May 02, 2005 at 14:26 UTC
    I'm a recent convert to HTML::Template and I think it may be of use here.

    For a loop it needs an array of hash refs.

    $sth = $dbh->prepare( 'SELECT * FROM guestbook' ); $sth->execute; my @ht_data; while( $row = $sth->fetchrow_hashref ){ push @ht_data, $row; } my $template = HTML::Template->new(filename => 'table.tmpl'); $template->param(TABLE_LOOP => \@ht_data); print $template->output();

    In your template the loop would look something like this

    <table class="user_form"> <TMPL_LOOP NAME=TABLE_LOOP> <tr> <td width="25%" class="name" > <TMPL_VAR NAME=NAME > </td> <td width="50%" class="email"> <TMPL_VAR NAME=EMAIL> </td> <td width="25%" class="date" > <TMPL_VAR NAME=DATE > </td> </tr> <tr> <td class="host"> Ευχή του Ιησού </td> <td class="tip" colspan="2"> <TMPL_VAR NAME=PRAY> </td> </tr> <tr> <td class="tip"> <TMPL_VAR NAME=NAME> </td> <td class="remark" colspan="2"> Προσωπική Ορθοδοξοπνευματική Εμπειρία </td> <td width="25%" class="date"> <TMPL_VAR NAME=DATE> </td> </tr> </TMPL_LOOP> </table>

    Untested. I don't have access to SQL. Hope this helps.

    A reply falls below the community's threshold of quality. You may see it by logging in.