Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Using the mysql database columns as the table header.

by mnlight (Scribe)
on Dec 27, 2001 at 11:15 UTC ( [id://134567]=perlquestion: print w/replies, xml ) Need Help??

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

How can I use the Columns from my table to populate the table header on my web page. Currently I am using 2 table functions one to make the table header the other to make the table data. The reason I seperate them is I am doing it in a while loop and if I put the header and data in the same table I will reproduce the header as many times as there is data in the array. Can I do this in one table and simply pull the column names right from 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(); }

Replies are listed 'Best First'.
Re: Using the mysql database columns as the table header.
by Spenser (Friar) on Dec 27, 2001 at 12:09 UTC

    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.

      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.

Re: Using the mysql database columns as the table header.
by japh (Friar) on Dec 27, 2001 at 11:28 UTC
    from perlman:lib:DBI:
    # NAME (array-ref, read-only) # Returns a reference to an array of field names for # each column. The names may contain spaces but should # not be truncated or have any trailing space. Note that # the names have the letter case (upper, lower or mixed) # as returned by the driver being used. Portable # applications should use the NAME_lc entry elsewhere in # this documentor the NAME_uc entry elsewhere in this # document. print "First column name: $sth->{NAME}->[0]\n";
    update: yes, that's perlman:lib:DBI and not :CGI, thanks dmmiller2k :)
      from perlman:lib:CGI

      I think perhaps you meant, perlman:lib:DBI.

      dmm

      I was picking some nits and discovered one of yours.
(jeffa) Re: Using the mysql database columns as the table header.
by jeffa (Bishop) on Dec 27, 2001 at 20:58 UTC
    This is exactly why i wrote DBIx::XHTML_Table. However, because you are detailing almost every single column, i really would recommend using HTML::Template for maintenance purposes.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)
    
Re: Using the mysql database columns as the table header.
by Alex the Serb (Monk) on Dec 27, 2001 at 11:57 UTC
    you should probably consider using the following things:

    Data::Table
    Data::ShowTable

    these are most helpful and beleve me there is no limit of what you can do with those! Before, when I did not know about these, I was kinda working by hand, then I've done the same thing with those and it came out nearly 100 lines smaller!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://134567]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-23 15:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found