Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Do you know where your variables are?
 
PerlMonks  

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

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

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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

Hi, I am retriving data from a table. I want to generate a html table to display the rows.The sql may return any # row and any # cols. Please help me to generate this. Thanks in advance,
  • 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 04: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 04: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 04: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 06: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 jonadab (Parson) on Nov 02, 2006 at 09: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.
Re: Build Table with 'n' rows and 'm' columns
by cLive ;-) (Prior) on Nov 02, 2006 at 10: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;

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
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.