将表格单元格与标题关联 [英] Associate table cell with header

查看:109
本文介绍了将表格单元格与标题关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



标题的大小来自SQL返回结果。然后把它放到一个数组中,

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

现在,如果詹姆斯的大小为S38。



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

  + -------- +  - ------ + -------- + ------- + ------- + ------- + ------- + 
| S36 | S37 | S38 | S39 | S40 | S41 | S42 |
+ -------- + -------- + -------- + ------- + ------- + - ----- + ------- +
| | |詹姆斯| | | | |
+ -------- + -------- + -------- + ------- + ------- + - ----- + ------- +

我知道如何做到这一点,如果大小是行或结果的一部分,但是作为表头?



如何使用Perl来操纵它?



编辑:

我尝试总结我试过的代码...

SQL查询:

 从文章where order_number =3中选择大小; 

进入数组:

<$ p $ ($ 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查询,我知道詹姆斯有S38。
如何放入上表的右列单元格。这将是:

  + -------- + -------- + --- ----- + ------- + ------- + ------- + ------- + 
| S36 | S37 | S38 | S39 | S40 | S41 | S42 |
+ -------- + -------- + -------- + ------- + ------- + - ----- + ------- +
| | |詹姆斯| | | | |
+ -------- + -------- + -------- + ------- + ------- + - ----- + ------- +


解决方案

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

 #!/ usr / bin / perl 

use strict;
使用警告;

使用CGI qw(:html);
使用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 

使用strict;使用警告;

使用HTML :: Template;
使用List :: AllUtils qw(first_index);

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

my @row =('& nbsp;')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;


I want to associate a cell value in table with it's header. The header is not known, as it was generated by SQL query.

Sizes as header came from SQL return result. So then put it into an array,

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

Now, if James has size S38.

I want to print them as HTML table with sizes header:

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

I know how to do this if sizes is part of row or result, but as table header?

How to manipulate this with Perl?

EDITED:

I try to summarize the code i tried...

SQL query:

select size from articles where order_number = "3";

Get into an array:

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

Say, @sizes is:

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

Create HTML header based on sizes:

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

Now, say from another SQL query, i know that James has S38. How to put into the right row cell of the above table. It would be:

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

解决方案

Here is a way of doing it using CGI.pm HTML generation methods:

#!/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;

On the other hand, I do love 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 = ('&nbsp;') 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;

这篇关于将表格单元格与标题关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆