in reply to Using the mysql database columns as the table header.

With the CGI.pm there are at least two ways to construct tables.  Rather than try to keep up with all of those brackets and parenthesis, try this format:

print $query->header(-type=>'text/html'), $query->start_html(-title=>'My Web Page'), $query->start_table({-border=>'0', -width=>'400'), $query->start_Tr, $query->start_td({-align=>'left', -width=>'100%', -colspan=>'1'), "Header", "\n", $query->end_td, $query->end_Tr; while(something) { somecode; somecode; # Loop around and fill in table data print $query->start_Tr, $query->start_td({-align=>'left', -width=>'100%', -colspan=>'1'), "Some Data", "\n", $query->end_td, $query->end_Tr; } #End while statement # Close out table and web page print $query->end_table, $query->end_html;

As you can see, you can start a table and table rows and table data cells without having to complete them.  This method allows your HTML calls within your while statement to act without consideration of the heading section of your table.  It also allows you to create the page with only one table.  Incidentally, there are more property settings to the table statement (i.e., cellpadding) and the start_html command;  and the start_td command doesn't require colspan to be set--I just wanted to show you how to do it.  I also should point out that for start_Tr and end_Tr, the "T" is capitalized.

That's Spenser, with an "s" like the detective.

Replies are listed 'Best First'.
Re: Re: Using the mysql database columns as the table header.
by mnlight (Scribe) on Dec 28, 2001 at 02:16 UTC
    I am trying the start_table approach as suggested by Spenser. So I decieded to just create a simple table using this method before I try to create one with a while loop embedded in it. I have this much written and it compiles but my table is just a line in the center. The data is not being written to the table can you tell me why?
    print $query->header(); print $query->start_html (-title=>'Roster of Players', -style=>{'src'=>'/CSS/stylesheet.css'}); print $query->h1({-align=>'center'}, '1990 MNLIGHTNING ROSTER'); print $query->start_table({-ALIGN=>'CENTER', BORDER=>''}),"\n"; print $query->start_Tr({-bgcolor=>'#99FF99', -valign=>'TOP', -a +lign=>'center'}); print $query->start_th({-width=>'150', -align=>'center', -cols +pan=>'2'}, 'COACHING STAFF','\n'); print $query->start_td({-width=>'150', -align=>'center', -co +lspan=>'2'}, 'HEAD COACH', 'Mike Schlagel','\n', 'ASST COACH', 'Jeff Jensen', '\n', 'ASST COACH', 'Lou Skarda', '\n', 'MANAGER', 'Chris Borer'); print $query->end_td; print $query->end_th; print $query->end_Tr; print $query->end_table; print $query->end_html;

      The start_td command is just like the <TD> command in raw HTML:  it doesn't contain actual text, just property settings like "align=left."  So, your code should look like this for a table data cell:

      print $query->start_th({-width=>'150', -align=>'center', -colspan=>'2'}), "COACHING STAFF","\n", $query->end_th;

      Of course, I'm not an expert at the CGI.pm and could very well be wrong about including text in the start_th.  But if you include the text in the directive, you may get yourself into the situation you were in before of directives getting intertwined with your while statement.  In short, I'm not giving you advanced advice here, but simple and managable ideas for keeping your sanity.  So, feel free to ignore me, as others will probably trump my post.

      That's Spenser, with an "s" like the detective.

        Why does this code not print the links below the table?
        print $query->table({-ALIGN=>'CENTER', BORDER=>''}, Tr({-bgcolor=>'#9933FF', -valign=>'TOP'}, [ th({-width=>'20%', -align=>'center'}, 'PLAYER NAME').th({-widt +h=>'30%', -align=>'center'}, 'ADDRESS').th({-width=>'20%', -al ign=>'center'}, 'PHONE NUMBER').th({-width=>'200', -align=>'center'}, +'FATHER').th({-width=>'200', -align=>'center'}, 'MOTHER').th({ -width=>'100', -align=>'center'}, 'DATE OF BIRTH').th({-width=>'100', +-align=>'center'}, 'ASSOCIATION').th({-width=>'50', -align=>'c enter'}, 'NUMBER').th({-width=>'50', -align=>'center'}, 'POSITION').th +({-width=>'25', -align=>'center'}, 'SHOOTS'), ] ) ); while (my @val = $sth->fetchrow_array ()) { print $query->table({-ALIGN=>'CENTER', BORDER=>''}, Tr({-bgcolor=>'#99FF99', -valign=>'TOP'}, [ td({-width=>'20%', -align=>'center'}, $val[0]).td({-width=>'30 +%', -align=>'center'}, $val[1]).td({-width=>'20%', -align=>'ce nter'}, $val[2]).td({-width=>'200', -align=>'center'}, $val[3]).td({-w +idth=>'200', -align=>'center'}, $val[4]).td({-width=>'100', -a lign=>'center'}, $val[5]).td({-width=>'100', -align=>'center'}, $val[6 +]).td({-width=>'50', -align=>'center'}, $val[7]).td({-width=>' 50', -align=>'center'}, $val[8]).td({-width=>'25', -align=>'center'}, +$val[9]), ] ) ); print $query->end_html(); } $query->start_table({-ALIGN=>'CENTER',}), $query->start_Tr({-bgcolor=>'#9933FF', -valign=>'TOP', -align= +>'center'}), $query->start_th({-align=>'center',}), "PLAYER NAME", $query->start_th({-align=>'center',}), "ADDRESS", $query->start_th({-align=>'center',}), "PHONE NUMBER", $query->start_th({-align=>'center',}), "FATHER", $query->start_th({-align=>'center',}), "MOTHER", $query->start_th({-align=>'center',}), "BIRTH-DATE", $query->start_th({-align=>'center',}), "ASSOCIATION", $query->start_th({-align=>'center',}), "NUMBER", $query->start_th({-align=>'center',}), "POSITION", $query->start_th({-align=>'center',}), "SHOOTS", $query->end_th, $query->end_Tr; while (my @val = $sth->fetchrow_array ()) { print $query->start_Tr({-bgcolor=>'#99FF99', -valign=>'TOP', -align= +>'center'}), $query->start_td({-align=>'center',}), $val[0], $query->start_td({-align=>'center',}), $val[1], $query->start_td({-align=>'center',}), $val[2], $query->start_td({-align=>'center',}), $val[3], $query->start_td({-align=>'center',}), $val[4], $query->start_td({-align=>'center',}), $val[5], $query->start_td({-align=>'center',}), $val[6], $query->start_td({-align=>'center',}), $val[7], $query->start_td({-align=>'center',}), $val[8], $query->start_td({-align=>'center',}), $val[9], $query->end_Tr; } $query->end_table, $query->a({-href=>'Roster.pl'}, h2('ROSTER'),), print $query->end_html;