For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > September 2004 > table with variables









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author table with variables
Ing. Branislav Gerzo

2004-09-12, 8:55 pm

Hi CGIers!

I have small question about putting variables into CGI script, which
produces table, here is snip of code:

use CGI qw(:standard);
#...
open FH, items.txt or die "Can't open $!";
my @items = sort <FH>;
print table(
{-border=>undef},
caption('Choose your favourite brand:'),
Tr({-align=>CENTER,-valign=>TOP},),
td($items[0], $items[1], $items[2]),
td($items[3], $items[4], $items[5])
);
#...

But how to do that inside while loop ? Can I use table() function, or
I have to print it (like without CGI module)?

Thanks for any help.


-=x=-
Skontrolované antivírovým programom NOD32

Gunnar Hjalmarsson

2004-09-12, 8:55 pm

Ing. Branislav Gerzo wrote:
> I have small question about putting variables into CGI script, which
> produces table, here is snip of code:
>
> use CGI qw(:standard);
> #...
> open FH, items.txt or die "Can't open $!";
> my @items = sort <FH>;
> print table(
> {-border=>undef},
> caption('Choose your favourite brand:'),
> Tr({-align=>CENTER,-valign=>TOP},),
> td($items[0], $items[1], $items[2]),
> td($items[3], $items[4], $items[5])
> );
> #...
>
> But how to do that inside while loop ? Can I use table() function, or
> I have to print it (like without CGI module)?


Maybe I have missed something... Why don't you just write code along
the line you want and try to figure out what you can and can't do? I
take for granted that you have enabled strictures and warnings, and
those will tell you about certain kind of mistakes.

Come back here *after having tried* if you encounter problems that you
are not able to resolve by help of the docs.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Charles K. Clarkson

2004-09-12, 8:55 pm

gerfobra@stonline.sk <gerfobra@stonline.sk> wrote:

: Hi CGIers!
:
: I have small question about putting variables into CGI
: script, which produces table, here is snip of code:
:
: use CGI qw(:standard);
: #...
: open FH, items.txt or die "Can't open $!";
: my @items = sort <FH>;
: print table(
: {-border=>undef},
: caption('Choose your favourite brand:'),
: Tr({-align=>CENTER,-valign=>TOP},),

That doesn't look right. Are CENTER and TOP constants?
If not, you don't have warnings turned on.


: td($items[0], $items[1], $items[2]),
: td($items[3], $items[4], $items[5])
: );
: #...
:
: But how to do that inside while loop? Can I use
: table() function, or I have to print it (like
: without CGI module)?

I don't understand the question. What while loop?
What would it be looping over?


HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328

Ing. Branislav Gerzo

2004-09-12, 8:55 pm

Charles K. Clarkson [CKC], on Thursday, September 9, 2004 at 17:37
(-0500) thinks about :

CKC> : print table(
: {-border=>>undef},
CKC> : caption('Choose your favourite brand:'),
CKC> : Tr({-align=>CENTER,-valign=>TOP},),

CKC> That doesn't look right. Are CENTER and TOP constants?
CKC> If not, you don't have warnings turned on.

i've turned warnings and everythings ok it prints me out:
<table border><caption>Choose your favourite brand:</caption>
<tr align="CENTER" valign="TOP" />


CKC> : td($items[0], $items[1], $items[2]),
CKC> : td($items[3], $items[4], $items[5])
CKC> : );
CKC> : #...
CKC> :
CKC> : But how to do that inside while loop? Can I use
CKC> : table() function, or I have to print it (like
CKC> : without CGI module)?

CKC> I don't understand the question. What while loop?
CKC> What would it be looping over?

I want print all items into table, table should have 3 columns.
I don't know how to do it.

Thanks and sorry for not clear question before.

--

...m8s, cu l8r, Brano.

[Dammit, Jim! I'm a meteorologist,not a forecaster!]



-=x=-
Skontrolované antivírovým programom NOD32

Charles K. Clarkson

2004-09-12, 8:55 pm

gerfobra@stonline.sk <gerfobra@stonline.sk> wrote:

: Charles K. Clarkson [CKC], on Thursday, September 9, 2004 at
: 17:37 (-0500) thinks about :
:
: : : print table(
: : {-border=>>undef},
: : : caption('Choose your favourite brand:'),
: : : Tr({-align=>CENTER,-valign=>TOP},),
:
: : That doesn't look right. Are CENTER and TOP constants?
: : If not, you don't have warnings turned on.
:
: i've turned warnings and everythings ok it prints me out:
: <table border><caption>Choose your favourite brand:</caption>
: <tr align="CENTER" valign="TOP" />

Hmph. You're right. It's a 'strict' error.

#!/usr/bin/perl

use strict;
use warnings;

use CGI 'Tr';

print Tr({-align=>CENTER,-valign=>TOP},);

__END__

Bareword "CENTER" not allowed while "strict subs" in use at aa.pl line 8.
Bareword "TOP" not allowed while "strict subs" in use at aa.pl line 8.


: : : td($items[0], $items[1], $items[2]),
: : : td($items[3], $items[4], $items[5])
: : : );
: : : #...
: : :
: : : But how to do that inside while loop? Can I use
: : : table() function, or I have to print it (like
: : : without CGI module)?
:
: : I don't understand the question. What while loop?
: : What would it be looping over?
:
: I want print all items into table, table should have 3 columns.
: I don't know how to do it.

But your file only seems to have 6 fields in it. Where is
the data for the other fields coming from and what is be looped
over in the while loop?

Closer inspection shows the above code doesn't work (right).
I think you wanted this.

use CGI qw( table Tr td caption );

my @items = ( 1 .. 6 );
print
table(
caption( 'Choose your favourite brand:' ),
Tr(
{ -align => 'CENTER',
-valign => 'TOP' },
td( @items[0 .. 2] ),
td( @items[3 .. 5] )
)
);


Which produces something like this (edited).

<table>
<caption>Choose your favourite brand:</caption>
<tr valign="TOP" align="CENTER">
<td>1 2 3</td>
<td>4 5 6</td>
</tr>
</table>



HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328




Charles K. Clarkson

2004-09-12, 8:55 pm

Charles K. Clarkson <mailto:cclarkson@htcomp.net> wrote:

: Closer inspection shows the above code doesn't work (right).
: I think you wanted this.
:
: use CGI qw( table Tr td caption );
:
: my @items = ( 1 .. 6 );
: print
: table(
: caption( 'Choose your favourite brand:' ),
: Tr(
: { -align => 'CENTER',
: -valign => 'TOP' },
: td( @items[0 .. 2] ),
: td( @items[3 .. 5] )
: )
: );

Or perhaps you are after this.

print
table(
caption( 'Choose your favourite brand:' ),
Tr(
{ -align => 'CENTER',
-valign => 'TOP' },
td( [ @items[0 .. 2] ] ),
),
Tr(
{ -align => 'CENTER',
-valign => 'TOP' },
td( [ @items[3 .. 5] ] )
)
);


Which produces something like this (edited).

<table>
<caption>Choose your favourite brand:</caption>
<tr valign="TOP" align="CENTER">
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr valign="TOP" align="CENTER">
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
</table>

:
:
: Which produces something like this (edited).
:
: <table>
: <caption>Choose your favourite brand:</caption>
: <tr valign="TOP" align="CENTER">
: <td>1 2 3</td>
: <td>4 5 6</td>
: </tr>
: </table>
:
:
:
: HTH,
:
: Charles K. Clarkson
: --
: Mobile Homes Specialist
: 254 968-8328


Bee

2004-09-12, 8:55 pm

>
> CKC> I don't understand the question. What while loop?
> CKC> What would it be looping over?
>
> I want print all items into table, table should have 3 columns.
> I don't know how to do it.
>
> Thanks and sorry for not clear question before.
>
> --


Do you mean you want a loop for rolling over all the contents in your array
?
Try this :

my @file = ('123', '333', 'abc', '13123', 'ccc', 'ddd', 'eee', 'fff',
'ggg');
my $WantColsByRow = 3; # Change this for how many cols you want to have
within a row
my $onCol = 0;

print "<table border=1>\n<tr>";
foreach my $data (@file)
{ if ($onCol == $WantColsByRow) { print "</tr>\n<tr>" ; $onCol = 0 } ;
print "<td>$data</td>";
$onCol ++
} print "</tr>\n</table>"


you got :
<table border=1>
<tr><td>123</td><td>333</td><td>abc</td></tr>
<tr><td>13123</td><td>ccc</td><td>ddd</td></tr>
<tr><td>eee</td><td>fff</td><td>ggg</td></tr>
</table>

CGI is good, but sometimes, that's better to choose a convinence way ,
rather than a standard way.


Sean Davis

2004-09-12, 8:55 pm

Just print '<table>' and '</table>' separately. Then, you are free to=20=

when necessary. Alternatively, if you are doing a good deal of this,=20
look at HTML::Template on cpan--quite nice for doing this type of=20
thing.

Sean

On Sep 9, 2004, at 6:20 PM, Ing. Branislav Gerzo wrote:

> Hi CGIers!
>
> I have small question about putting variables into CGI script, which
> produces table, here is snip of code:
>
> use CGI qw(:standard);
> #...
> open FH, items.txt or die "Can't open $!";
> my @items =3D sort <FH>;
> print table(
> {-border=3D>undef},
> caption('Choose your favourite brand:'),
> Tr({-align=3D>CENTER,-valign=3D>TOP},),
> td($items[0], $items[1], $items[2]),
> td($items[3], $items[4], $items[5])
> );
> #...
>
> But how to do that inside while loop ? Can I use table() function, or
> I have to print it (like without CGI module)?
>
> Thanks for any help.
>
>
> -=3Dx=3D-
> Skontrolovan=E9 antiv=EDrov=FDm programom NOD32
>
>
> --=20
> To unsubscribe, e-mail: beginners-cgi-unsubscribe@perl.org
> For additional commands, e-mail: beginners-cgi-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>


Bob Showalter

2004-09-12, 8:55 pm

Sean Davis wrote:
> Just print '<table>' and '</table>' separately.


Note that CGI has start_table and end_table methods. If you use function
style, you need to import them:

use CGI qw(:standard start_table);

print start_table;
...
print end_table;

Actually, you can do this with any tag, not just table. Also note that
importing start_sometag also imports the corresponding end_sometag.

Bob Showalter

2004-09-12, 8:55 pm

Ing. Branislav Gerzo wrote:
>
> I want print all items into table, table should have 3 columns.
> I don't know how to do it.


What you have prints one row with two columns. It looks like perhaps you
want two rows with three columns in each row.

To simplify, here's what you have:

print Tr(td(0, 1, 2), td(3, 4, 5))

Which emits:

<tr><td>0 1 2</td> <td>3 4 5</td></tr>

What you should do is pass an array reference to both Tr() and td(), which
causes them to emit separate elements for each entry in the array:

print Tr([ td([ 0, 1, 2 ]), td([ 3, 4, 5 ]) ])

Which emits:

<tr><td>0</td> <td>1</td> <td>2</td></tr>
<tr><td>3</td> <td>4</td> <td>5</td></tr>

Note how each element is now inside it's own cell.

Now, if the number of entries in @items is variable, you need to print
enough rows of three elements each in order to display the entire array.

Here's a little script to illustrate:

#!/usr/bin/perl
use strict;
use CGI ':standard';
my @items = map "value $_", 1..10;
my $ncol = 3;
print Tr([ map td([ @items[ $ncol * $_ .. $ncol * $_ + $ncol - 1 ] ]),
0 .. @items / $ncol]);

That last line is a bit complex, so let me break it down:

print Tr([ ...some rows... ]);

The square brackets indicates an array reference. This causes CGI to output
a <tr> element for each entry in "some stuff". So "some rows" is a list of
rows in the table.

The rows are formed by:

map ...a row... , 0 .. @items / $ncol

Perl's map() function returns a list, by iterating over the list 0 .. @items
/ $ncol and evaluating "a row" for each value in that list. @items / $ncol
is 10 / 3, which is 3.3333, so the range is 0 .. 3.3333, which perl treats
as 0 .. 3, or 0, 1, 2, 3. These are basically the row numbers in the table.
Since we have ten elements to display in three columns, it will take four
rows to display them (0 .. 3).

The map() call then needs to output a string of HTML representing the
contents of a single row (whose row number is in $_). That's what "a row"
needs to do.

The code to output the HTML for a single row is:

td([ @items[ $ncol * $_ .. $ncol * $_ + $ncol - 1 ] ])

Again, the outermost set of square brackets supplies an anonymous array to
td(), which causes CGI to output a separate <td> element for each entry in
the array. The array is a slice from @items consisting of the elements for
the row number in $_. If the row number is 0, we want elements 0, 1, and 2.
If the row number is 2, we want elements 6, 7, and 8. The starting element
is always $ncol * $_, and the ending element is ($ncol - 1) beyond that.

If you can get all the maths down right, this kind of thing is extremely
powerful for generating dynamic tables, forms, etc. Take a look at "perldoc
CGI" under the heading "THE DISTRIBUTIVE PROPERTY OF HTML SHORTCUTS"
Alexander Kleshchevnikov

2004-09-14, 3:55 am

Here's my offer (you can change $num_items_in_td and $num_cols):

use CGI qw(:standard);
open (FH, "items.txt") or die "Can't open file. Cause: $!";
my @items = sort <FH>;
chomp(@items);
print header();

$num_cols = 2;
$num_items_in_td = 2;

for ($i = 0; defined($items[$i]); $i += $num_items_in_td) {
push @tds, td(@items[$i..($i+$num_items_in_td-1)]);
if ($i+$num_items_in_td > $#items || @tds % $num_cols == 0) {
push @trs, Tr({-align=>CENTER,-valign=>TOP}, @tds);
@tds = ();
}
}

print table(
{-border=>undef},
caption('Choose your favourite brand:'),
@trs
);

-------------------------------
Alexander Kleshchevnikov,
DirectEDI Developer
email: seigo_ua@yahoo.com
icq: 153617972
-------------------------------


"Ing. Branislav Gerzo" <konfera@2ge.us> wrote in message
news:221776018.20040910002030@2ge.us...
> Hi CGIers!
>
> I have small question about putting variables into CGI script, which
> produces table, here is snip of code:
>
> use CGI qw(:standard);
> #...
> open FH, items.txt or die "Can't open $!";
> my @items = sort <FH>;
> print table(
> {-border=>undef},
> caption('Choose your favourite brand:'),
> Tr({-align=>CENTER,-valign=>TOP},),
> td($items[0], $items[1], $items[2]),
> td($items[3], $items[4], $items[5])
> );
> #...
>
> But how to do that inside while loop ? Can I use table() function, or
> I have to print it (like without CGI module)?
>
> Thanks for any help.
>
>
> -=x=-
> Skontrolované antivírovým programom NOD32
>



Ing. Branislav Gerzo

2004-09-14, 3:55 am

Bob Showalter [BS], on Friday, September 10, 2004 at 10:33 (-0400)
wrote these comments:
[color=darkred]
BS> What you have prints one row with two columns. It looks like perhaps you
BS> want two rows with three columns in each row.

yes, you are right. I want print some rows with three columns, rows
are ofcourse depend on input data.

BS> #!/usr/bin/perl
BS> use strict;
BS> use CGI ':standard';
BS> my @items = map "value $_", 1..10;
BS> my $ncol = 3;
BS> print Tr([ map td([ @items[ $ncol * $_ .. $ncol * $_ + $ncol - 1 ] ]),
BS> 0 .. @items / $ncol]);

very very nice clear script! I did play it it and the map function is
so powerful in this way.

BS> If you can get all the maths down right, this kind of thing is extremely
BS> powerful for generating dynamic tables, forms, etc. Take a look at "perldoc
BS> CGI" under the heading "THE DISTRIBUTIVE PROPERTY OF HTML SHORTCUTS"

thanks for nice answer, I did change my routine with this one, and now
the code is about 10 lines shorter :)
Thanks again.

--

...m8s, cu l8r, Brano.

[Old sci-fi actors never die, they just go to Babylon Five.]



-=x=-
Skontrolované antivírovým programom NOD32

Alexander Kleshchevnikov

2004-09-14, 3:55 am

Here's my offer (you can change $num_items_in_td and $num_cols):

use CGI qw(:standard);
open (FH, "items.txt") or die "Can't open file. Cause: $!";
my @items = sort <FH>;
chomp(@items);
print header();

$num_cols = 4;
$num_items_in_td = 5;

for ($i = 0; defined($items[$i]); $i += $num_items_in_td) {
push @tds, td(@items[$i..($i+$num_items_in_td-1)]);
if ($i != 0 && ($i+$num_items_in_td > $#items || @tds % $num_cols == 0)) {
push @trs, Tr({-align=>CENTER,-valign=>TOP}, @tds);
@tds = ();
}
}

print table(
{-border=>undef},
caption('Choose your favourite brand:'),
@trs
);

--
Seigo

"Ing. Branislav Gerzo" <konfera@2ge.us> wrote in message
news:221776018.20040910002030@2ge.us...
> Hi CGIers!
>
> I have small question about putting variables into CGI script, which
> produces table, here is snip of code:
>
> use CGI qw(:standard);
> #...
> open FH, items.txt or die "Can't open $!";
> my @items = sort <FH>;
> print table(
> {-border=>undef},
> caption('Choose your favourite brand:'),
> Tr({-align=>CENTER,-valign=>TOP},),
> td($items[0], $items[1], $items[2]),
> td($items[3], $items[4], $items[5])
> );
> #...
>
> But how to do that inside while loop ? Can I use table() function, or
> I have to print it (like without CGI module)?
>
> Thanks for any help.
>
>
> -=x=-
> Skontrolované antivírovým programom NOD32
>



Alexander Kleshchevnikov

2004-09-14, 3:55 am

Sorry, I find out bug in previous code. Use this:

use CGI qw(:standard);
open (FH, "items.txt") or die "Can't open file. Cause: $!";
my @items = sort <FH>;
chomp(@items);
print header();

$num_cols = 2;
$num_items_in_td = 2;

for ($i = 0; defined($items[$i]); $i += $num_items_in_td) {
push @tds, td(@items[$i..($i+$num_items_in_td-1)]);
if ($i+$num_items_in_td > $#items || @tds % $num_cols == 0) {
push @trs, Tr({-align=>CENTER,-valign=>TOP}, @tds);
@tds = ();
}
}

print table(
{-border=>undef},
caption('Choose your favourite brand:'),
@trs
);

--
Seigo

"Ing. Branislav Gerzo" <konfera@2ge.us> wrote in message
news:221776018.20040910002030@2ge.us...
> Hi CGIers!
>
> I have small question about putting variables into CGI script, which
> produces table, here is snip of code:
>
> use CGI qw(:standard);
> #...
> open FH, items.txt or die "Can't open $!";
> my @items = sort <FH>;
> print table(
> {-border=>undef},
> caption('Choose your favourite brand:'),
> Tr({-align=>CENTER,-valign=>TOP},),
> td($items[0], $items[1], $items[2]),
> td($items[3], $items[4], $items[5])
> );
> #...
>
> But how to do that inside while loop ? Can I use table() function, or
> I have to print it (like without CGI module)?
>
> Thanks for any help.
>
>
> -=x=-
> Skontrolované antivírovým programom NOD32
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com