Embperl - building dynamic websites with Perl


Dynamic Tables
[ << Prev: How to Embed Perl Code in HTML Documents ] [ Content ] [ Next: Form fields >> ]

A very powerful feature of Embperl is the processing of dynamic tables. This feature was designed mainly to display Perl arrays (one or two dimensional, regular and irregular), but can also be used in other ways.



Display a Perl Arraytop
 [- @a = ( 'A', 'B', 'C') ; -]
 <TABLE BORDER=1>
   <TR>
        <TD> [+ $a[$row] +] </TD>
   </TR>
 </TABLE>

The above example simply displays a table with three rows containing A, B and C.

The trick is done by using the magical variable $row which contains the row count and is incremented for every row. The table ends if the expression which contains $row returns <undef>. The same can be done with $col for columns and $cnt can be used if you need a table which wraps after a certain number of elements.

This works with table/select/menu/ol/dl/dir



Simple DBI Exampletop

Here is a simple DBI example that displays the result of a query as a two dimension table, with field names as headings in the first row:

 [-
 # connect to database
  $dbh = DBI->connect($DSN) ;

 # prepare the sql select
 $sth = $dbh -> prepare ("SELECT * from $table") ;

 # execute the query
 $sth -> execute ;

 # get the fieldnames for the heading in $head
 $head = $sth -> {NAME} ;

 #continues on the next page...
 # get the result in $dat 
 $dat = $sth -> fetchall_arrayref ;
 -]

 <table>
    <tr><th>[+ $head->[$col] +]</th></tr>
    <tr><td>[+ $dat -> [$row][$col] +]</td></tr>
 </table>

[ << Prev: How to Embed Perl Code in HTML Documents ] [ Content ] [ Next: Form fields >> ]


© 1997-2023 Gerald Richter / actevy