in reply to using 'use CGI;' and tables

Statically:

print($q->table( $q->tr( $q->td("row 1, col 1"), $q->td("row 1, col 2"), ), $q->tr( $q->td("row 2, col 1"), $q->td("row 2, col 2"), ), ));

Dynamically:

my $table = [ [ 'row 1, col 1', 'row 1, col 2', ], [ 'row 2, col 1', 'row 2, col 2', ], ]; { my $table_body = '' foreach (@$table) { my $row_body = ''; foreach (@$_) { $row_body .= $q->td($_); } $table_body .= $q->tr($row_body); } print($q->table($table_body)); }

Replies are listed 'Best First'.
Re^2: using 'use CGI;' and tables
by bscruggs99 (Initiate) on Apr 13, 2006 at 05:00 UTC
    Thanks ikegami, one question though. With the dynamic example, where am I supposed to put that? The code I'm using for my table at the moment is
    while( $row = $sth_sel->fetch()) { $sth_sel->bind_col(1, \$firstname); $sth_sel->bind_col(2, \$lastname); $sth_sel->bind_col(3, \$state); $sth_sel->bind_col(4, \$country); while ( $sth_sel->fetch() ) { print $q->table({-border=>'1', -width=>'560', -cellpadding=>'0', -cell +spacing=>'0'}, $q->Tr({-align=>'CENTER'}, [ $q->td({-width=>'140', -align=>'center'},[$firstname, $lastnam +e, $state, $country]) ] ) ); } }
    I'm pretty new to Perl and I just don't quite grasp where I would put the dynamic example in what I have already.

      If there's anything you should have learned from my previous code, it's that table should be called *outside* of the row loop, and tr should be called *outside* of the column loop. In context, you get:

      $sth_sel->bind_col(1, \$firstname); $sth_sel->bind_col(2, \$lastname ); $sth_sel->bind_col(3, \$state ); $sth_sel->bind_col(4, \$country ); my $user_table_body = ''; while ($sth_sel->fetch()) { $user_table_body = $q->tr( { -class => 'user_row' }, $q->td({ -class => 'user_firstname_cell' }, $firstname), $q->td({ -class => 'user_lastname_cell' }, $lastname), $q->td({ -class => 'user_state_cell' }, $state), $q->td({ -class => 'user_country_cell' }, $country), ); } print $q->table( { -class => 'user_table' }, $user_table_body );

      I fixed the very wrongly nested loops (by simply removing the extraneous outside loop). Also, I removed the stuff that belongs in CSS, and I added classes to facilitate the use of CSS.

      OK,I got it to display the table, but how do I set the table attributes like width, etc...? Also, it's only displaying the data that was entered in the form. It's currently displaying all the info from the database. What do I need to modify to display all the info from the database?

        Add the following to your HTML's HEAD element:

        <style type="text/css"> .user_table { border-collapse: collapse; border: solid 1px; padding: 0; } .user_row { border: solid 1px; } .user_firstname_cell, .user_lastname_cell, .user_state_cell, .user_country_cell { border: solid 1px; padding: 0; width: 140; text-align: center; } </style>

        Reference

        Updated