2009-02-23 6 views
2

Je reçois des données d'une base de données. Il a 4 lignes et 6 colonnes. Je veux montrer cela dans un tableau HTML mais j'ai des problèmes. J'ai fait le TH pour les 6 colonnes. Cependant, j'ai des problèmes pour afficher les 4 lignes. Voilà ce que j'ai jusqu'à présent:Comment puis-je afficher des données dans une table avec Perl?

while (($f1, $t2, $n3, $k4, $d5, $e6) = $sth1->fetchrow_array) 
{ 
     push (@first, $f1); 
     push (@second, $t2); 
     push (@third, $n3); 
     push (@fourth, $k4); 
     push (@fifth, $d5); 
     push (@sixth, $e6); 
} 

@first, @second ... et d'autres sont des tableaux

Bien que les données dsplaying si je fais:

foreach (@first) 
{ 
     print "<td>$_</td>"; 
} 

qui affiche les données verticalement mais je veux qu'il affiche horizontalement.

Répondre

5

Le code de la question est écrite à l'aide DBI de Perl (DataBase Interface):

print "<table>\n"; 
while (($f1, $t2, $n3, $k4, $d5, $e6) = $sth1->fetchrow_array) 
{ 
    print " <tr>\n"; 
    print " <td>$f1</td>\n"; 
    print " <td>$t2</td>\n"; 
    print " <td>$n3</td>\n"; 
    print " <td>$k4</td>\n"; 
    print " <td>$d5</td>\n"; 
    print " <td>$e6</td>\n"; 
    print " </tr>\n"; 
} 
print "</table>\n"; 

Ou vous pouvez lire la ligne dans un tableau, puis imprimer le tableau plus succinctement:

print "<table>\n"; 
while (my(@row) = $sth1->fetchrow_array) 
{ 
    print " <tr>\n"; 
    print " <td>$val</td>\n" foreach my $val (@row); 
    print " </tr>\n"; 
} 
print "</table>\n"; 
1

Est-ce plus proche de ce que vous essayez d'accomplir?

# display one HTML table row for each DB row 
print "<table>\n"; 
while (my @row = $sth1->fetchrow_array) { 
    print "<tr>".join("", map { "<td>${_}</td>" } @row)."</tr>\n"; 
} 
print "</table>\n"; 
+0

Il est Perl - TMTOWTDI! –

8

Vous pouvez également utiliser HTML::Table.

use strict; 
use warnings; 
use HTML::Table; 

my $table = HTML::Table->new( 
    -cols => 6, 
    -border => 1, 
    -padding => 1, 
    -head => [qw(f1 t2 n3 k4 d5 e6)], 
); 

# do query here.  

while (my @row = $sth->fetchrow_array) { 
    $table->addRow(@row); 
} 

$table->print; 
+0

ce code n'est même pas compilé. – Jill448

+0

@ Jill448, vous devez faire la requête de base de données et obtenir un handle d'instruction, '$ sth'. Ce code doit être dans la section '# query here'. Vous devrez également ajouter le DBI. – daotoad

2

Si vous voulez un peu plus de flexibilité sans avoir à écrire du code autant, vous pouvez également consulter HTML :: Table :: FromDatabase. Il fait le HTML pour vous et vous donne la liberté de formater les choses facilement. Il faut un peu de pratique, mais c'est devenu un outil précieux dans mon arsenal.

Voici quelques exemples de code qui vous donnera une idée de ce que vous pouvez faire avec

use DBI; 
use HTML::Table::FromDatabase; 
use HTML::Table; 
use CGI; 

# Start the CGI wrapper 
$query = new CGI(); 

# Just build the STH like normal, with either the SQL in-line or an $sql variable 
my $sth = $mysql_dbh->prepare("SELECT DISTINCT field1, field2 FROM table ORDER BY field1"); 
$sth->execute; 

# Tell the module to build the table using the STH you executed before 
my $table = HTML::Table::FromDatabase->new(-sth=>$sth, 
              -border=>1, # We want a border on the entire table 
              -callbacks=>[ 
               { 
                 # On the switch column, we want to add an HREF with the data in the URL 
                 column=>'field1', 
                 transform=> 
                   sub { $_ = shift; qq[<A HREF="$_">$_</A>]; }, 
               }, 
               { 
                 # This example lets you return a value based on what value is in the field 
                 column=>'field2', 
                 transform=> 
                   sub { $text = shift; DidItMatch($text); }, 
               }], 
              # The two rows below are used to set CSS classes on even and odd rows if you want different formatting 
              -evenrowclass=>'evenrow', 
              -oddrowclass=>'oddrow' 
              ); 

# Take the entire table and align all of the 2nd column to center 
$table->setColAlign(2,'center'); 

# The following lines are just demonstrations of what else can be changed. There are a lot, so look at HTML::Table to get all of them 
#$table->setColBGColor(2,'red');    # Sets the background color of all cells in a column 
#$table->SetColVAlign(2,'middle');    # Sets the vertical alignment of all cells in a column 
#$table->setColWidth(2,100);     # Sets the width of a column. Could also use setColWidth(2,'50%'); 
#$table->setColNoWrap(2,1);      # Sets the no-wrap property of a colum. 0=Wrap 1=NoWrap 

# The following lines allow you to retrieve information about the table that was created. 
my $row_count = $table->getTableRows;   # Retrieves the number of rows in the table 
my $column_count = $table->getTableCols;  # Retrieves the number of columns in the table 

# Finally, print the finalized table HTML 
$table->print; 

# Wrap up the STH and DBH if needed 
$sth->finish; 
$mysql_dbh->disconnect; 


sub DidItMatch 
{ 
     my $text = shift; 
     if ($text eq 'Good text') 
     { 
       return "Matched"; 
     } else 
     { 
       return "$text"; 
     } 
} 
Questions connexes