Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Build Table with 'n' rows and 'm' columns

by Anonymous Monk
on Nov 02, 2006 at 08:56 UTC ( [id://581852]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on Build Table with 'n' rows and 'm' columns

Replies are listed 'Best First'.
Re: Build Table with 'n' rows and 'm' columns
by GrandFather (Saint) on Nov 02, 2006 at 09:10 UTC

    Which of the many HTML modules or templating modules are you using at present and where are you having trouble with whatever you are using?

    If you are not using any such tool at present perhaps you should at least mention the copntext for the question. It's pretty unusual for HTML tables to stand on their own and the rest of the task will influence what is most appropriate for your task.

    You might like to start by looking at modules such as Template toolkit (for heavy lifting) or HTML::DBTable, HTML::Table, HTML::STable, HTML::HashTable or HTML::QuickTable (selected at - almost - random from them many available on CPAN).


    DWIM is Perl's answer to Gödel
Re: Build Table with 'n' rows and 'm' columns
by Samy_rio (Vicar) on Nov 02, 2006 at 09:19 UTC

    Hi, Try like this,

    use strict; use warnings; use DBI; use HTML::TableTiler; my @matrix; my $dbh = DBI->connect( "DBI:mysql:project") or die "Can't connect to +Oracle database: $DBI::errstr\n"; my $sth = $dbh->prepare("select * FROM modules"); $sth->execute || $sth->errstr(); while (my @row = $sth->fetchrow_array) { push (@matrix, [@row]); } $dbh->disconnect; my $tt = HTML::TableTiler->new(); print $tt->tile_table(\@matrix);

    Thanks ikegami++

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

      There room for a some small improvements.
      • You don't check the result of $dbh->prepare.
      • You don't check the result of $dbh->fetchrow_array.
      • Your loop can be replaced with $sth->fetchall_arrayref.
      • You can also condense the code further using $dbh->selectall_arrayref.
      use strict; use warnings; use DBI (); use HTML::TableTiler (); my $dbh = DBI->connect("DBI:mysql:project") or die "Can't connect to Oracle database: $DBI::errstr\n"; my $matrix = $dbh->selectall_arrayref("SELECT * FROM modules"); die "Unable to fetch query results: $DBI::errstr\n" if $dbh->err; my $tt = HTML::TableTiler->new(); print $tt->tile_table($matrix);

      Update: I was obviously tired! Fixed the problems identified in replies.

        there is still room for some small improvement!
        Global symbol "$sth" requires explicit package name
        ;-)
      Ha Ha I like they way you mess with your users: failure to connect to a mysql database sends them looking for a failed Oracle instance.
      Evil genius!
      my $dbh = DBI->connect( "DBI:mysql:project") or die "Can't connect to +Oracle database: $DBI::errstr\n";

      Update: Added code for quick reference.

      andyford
      or non-Perl: Andy Ford

Re: Build Table with 'n' rows and 'm' columns
by msbalaji (Chaplain) on Nov 02, 2006 at 09:30 UTC

    Hi,

    By using the below statement you can create html table

    mysql -H -e "SELECT * FROM tablename" databasename

    regards
    Balaji. M

      How do you know she is a witch? Why mysql? Should Anonymous Monk make a system call to run this?
Re: Build Table with 'n' rows and 'm' columns
by fenLisesi (Priest) on Nov 02, 2006 at 11:28 UTC
    DBIx::XHTML_Table could help, but I do not understand "any number of columns". Are you dynamically adding columns to your tables?
Re: Build Table with 'n' rows and 'm' columns
by cLive ;-) (Prior) on Nov 02, 2006 at 15:59 UTC
    TIMTOWTDI ;-)
    #!/usr/bin/perl use strict; use warnings; use CGI; my $data = [ [1,23,3],[4,5,6],[2,3,6],[7,3,7] ]; my $q = CGI->new(); print $q->header. $q->start_html. $q->table( $q->Tr( [ map { $q->td($_) } @{$data} ] ) ). $q->end_html;
Re: Build Table with 'n' rows and 'm' columns
by jonadab (Parson) on Nov 02, 2006 at 14:05 UTC
    I would probably use map and join, but you could just as easily use foreach and concatenation. Either way you can use HTML::Entities for escaping. Which part are you having trouble with? Can you show us what you've tried so far? We could probably give you better, more specific help if you ask a more specific question.

    Sanity? Oh, yeah, I've got all kinds of sanity. In fact, I've developed whole new kinds of sanity. Why, I've got so much sanity it's driving me crazy.

Log In?
Username:
Password:

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

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

    No recent polls found