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

Hello, this question is almost embarassing to ask, but I can't seem to get it worked out. The script I'm working on now takes user input from a form then reads from a database and displays the entire updated contents of it in a table. The only problem I'm having with it now is that instead of just repeating the TR and TD tags in the table, it repeats the whole table so instead of
<table> <tr> <td></td> </tr> <tr> <td></td> </tr> </table>
I have
<table> <tr> <td></td> </tr> </table> <table> <tr> <td></td> </tr> </table>
etc... I've tried moving print $q->table({-border=>'1', -width=>'560', -cellpadding=>'0', -cellspacing=>'0'}, but no matter where I've put it, all it's given me is errors. There has to be an easy way of doing this that I'm not seeing. If anybody could point me in the right direction, I'd greatly appreciate it.

Replies are listed 'Best First'.
Re: using 'use CGI;' and tables
by ikegami (Patriarch) on Apr 13, 2006 at 04:52 UTC

    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)); }
      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?
Re: using 'use CGI;' and tables
by jesuashok (Curate) on Apr 13, 2006 at 04:33 UTC
    Hi

    please throw all this printings and use
    HTML::Template
    This will improve the performance also
    cheers

    "Keep pouring your ideas"