【问题标题】:Associate table cell with header将表格单元格与标题关联
【发布时间】:2009-10-29 02:17:06
【问题描述】:

我想将表格中的单元格值与其标题相关联。标题未知,因为它是 由 SQL 查询生成。

作为标头的大小来自 SQL 返回结果。那么然后把它放入一个数组中,

@sizes = qw(S36 S37 S38 S39 S40 S41 S42);

现在,如果詹姆斯的尺码为 S38。

我想将它们打印为带有尺寸标题的 HTML 表格:

+--------+--------+--------+-------+-------+-------+-------+
|    S36 |    S37 |    S38 |   S39 |   S40 |   S41 |   S42 |
+--------+--------+--------+-------+-------+-------+-------+
|        |        |  James |       |       |       |       |
+--------+--------+--------+-------+-------+-------+-------+

如果尺寸是行或结果的一部分,但作为表头,我知道该怎么做?

如何用 Perl 来操作它?

已编辑:

我试着总结一下我试过的代码……

SQL 查询:

select size from articles where order_number = "3";

进入一个数组:

while(my $ref = $sth->fetchrow_hashref()) {
    $size = "$ref->{'size'}";
    push @sizes, $size;
}

说,@sizes 是:

@sizes = qw(S36 S37 S38 S39 S40 S41 S42);

根据尺寸创建 HTML 标头:

+--------+--------+--------+-------+-------+-------+-------+
|    S36 |    S37 |    S38 |   S39 |   S40 |   S41 |   S42 |
+--------+--------+--------+-------+-------+-------+-------+

现在,从另一个 SQL 查询说,我知道 James 有 S38。 如何放入上表的右行单元格。应该是:

+--------+--------+--------+-------+-------+-------+-------+
|    S36 |    S37 |    S38 |   S39 |   S40 |   S41 |   S42 |
+--------+--------+--------+-------+-------+-------+-------+
|        |        |  James |       |       |       |       |
+--------+--------+--------+-------+-------+-------+-------+

【问题讨论】:

  • 您的问题有点难以回答。你有什么代码可以给我们看吗?也许如果您尝试做某事,然后在遇到困难时提出问题?

标签: sql perl header


【解决方案1】:

这是一种使用CGI.pm HTML 生成方法的方法:

#!/usr/bin/perl

use strict;
use warnings;

use CGI qw(:html);
use List::AllUtils qw( first_index );

my @sizes = qw(S36 S37 S38 S39 S40 S41 S42);
my %person = ( name => 'James', size => 'S38');

my @row = ('') x @sizes;
$row[ first_index { $_ eq $person{size} } @sizes ] = $person{name};

print start_html,
      table( { border => 1 },
          Tr(td({width => sprintf('%.0f%%', 100/@sizes)}, \@sizes)),
          Tr(td(\@row) ) ),
      end_html;

另一方面,我很喜欢HTML::Template

#!/usr/bin/perl

use strict; use warnings;

use HTML::Template;
use List::AllUtils qw( first_index );

my @sizes = qw(S36 S37 S38 S39 S40 S41 S42);
my %person = ( name => 'James', size => 'S38');

my @row = (' ') x @sizes;
$row[ first_index { $_ eq $person{size} } @sizes ] = $person{name};

my $tmpl_txt = <<EO_TMPL;
<html><head><style type="text/css">
#size_chart { margin: 0 auto; }
#size_chart td { width: <TMPL_VAR CELL_WIDTH>; border: 2px inset #ddd }
</style></head>
<body><table id="size_chart">
<TMPL_LOOP ROWS><tr>
<TMPL_LOOP CELLS><td><TMPL_VAR CELL></td></TMPL_LOOP>
</tr></TMPL_LOOP>
</table></body></html>
EO_TMPL

my $tmpl = HTML::Template->new(scalarref => \$tmpl_txt);
$tmpl->param(
    CELL_WIDTH => sprintf('%.0f%%', 100/@sizes),
    ROWS => [ { CELLS => [ map { { CELL => $_ } } @sizes ] },
              { CELLS => [ map { { CELL => $_ } } @row ]   },
]);

print $tmpl->output;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-24
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    相关资源
    最近更新 更多