Home : Introduction : Embperl
Google Web www.embperl.org

Embperl 2 Advanced
More infos
Add info about Embperl

    Stable 2.4.0
    Beta 2.5.0_3
Support the development of Embperl! More...
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') ; -]
        <TD> [+ $a[$row] +] </TD>

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

 # excute 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 ;

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

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

© 1997-2012 Gerald Richter / ecos gmbh